Repository navigation
Announcement bar with a new id stays hidden on first page load for users who dismissed the previous one #12646
Description
Have you read the Contributing Guidelines on issues?
- I have read the Contributing Guidelines on issues.
Prerequisites
- I'm using the latest version of Docusaurus.
- I have tried the
npm run clearoryarn clearcommand. - I have tried
rm -rf node_modules yarn.lock package-lock.jsonand re-installing packages. - I have tried creating a repro with https://new.docusaurus.io.
- I have read the console error message carefully (if applicable).
Description
When a user has dismissed an announcement bar and the site then ships a new announcement with a different themeConfig.announcementBar.id, the new bar is not shown on the first page load. It appears only after a second load.
The id is documented as the way to re-show the bar to users who closed the previous one, so the new announcement should be visible right away.
Cause. Two pieces of code decide visibility, and only one of them checks the id:
- Inline script, before hydration.
getAnnouncementBarInlineScriptsets<html data-announcement-bar-initially-dismissed>fromdocusaurus.announcement.dismissalone. It never readsdocusaurus.announcement.id, so after the previous bar was dismissed it setstrue. - CSS.
html[data-announcement-bar-initially-dismissed='true'] .announcementBar { display: none; }intheme-classic/src/theme/AnnouncementBar/styles.module.csshides the bar. - React, after hydration. The
AnnouncementBarProvidereffect sees that theidchanged. It writesdismiss = falseand callssetClosed(false), soisActivebecomestrueand the bar is rendered. - Nothing resets the
data-announcement-bar-initially-dismissedattribute. The CSS rule from step 2 still applies, so the rendered bar stays invisible.
On the next load, dismiss is already false, the inline script sets the attribute to false, and the bar finally shows.
Proposed fix. Make the inline script check the id too: pass the configured announcementBar.id to getAnnouncementBarInlineScript (from theme-classic/src/index.ts), and treat the bar as not dismissed when the stored docusaurus.announcement.id differs from it. I'll open a PR with this change and a test.
Reproducible demo
No response
Steps to reproduce
- Create a fresh classic site and set in
docusaurus.config.js:themeConfig: { announcementBar: { id: 'announcement-1', content: 'First announcement', isCloseable: true, }, },
- Run
npm run build && npm run serveand open the site. - Close the announcement bar.
- Change the config to
id: 'announcement-2',content: 'Second announcement'. Rebuild and serve again. - Reload the page.
The same happens with npm run start.
Expected behavior
The second announcement is visible on the first load after step 5, because its id differs from the dismissed one.
Actual behavior
On the first load after step 5:
- The bar is not visible.
localStoragealready holdsdocusaurus.announcement.id = "announcement-2"anddocusaurus.announcement.dismiss = "false".<html>still hasdata-announcement-bar-initially-dismissed="true".- The
.announcementBarelement is in the DOM but hidden bydisplay: none.
The bar appears only after a second reload. Users who visit once after an announcement changes never see it.
Your environment
- Docusaurus version used: 3.10.2 (the code is the same on
mainatc245217) - Environment name and version (e.g. Chrome 89, Node.js 16.4): any browser; Node.js 24.18.0
- Operating system and version (e.g. Ubuntu 20.04.2 LTS): macOS 15.7.4 (not OS-specific)
Self-service
- I'd be willing to fix this bug myself.
Activity