Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CSS modules ordering #8877

Merged
merged 2 commits into from
Oct 23, 2023
Merged

Fix CSS modules ordering #8877

merged 2 commits into from
Oct 23, 2023

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Oct 20, 2023

Changes

Fix #8558

Fix CSS modules ordering by rendering styles first before links. This is needed because in getStylesForURL, CSS modules are passed as links (importedCssUrls) (CSS module has no ssrModule.default):

if (
mode === 'development' && // only inline in development
typeof ssrModule?.default === 'string' // ignore JS module styles
) {
importedStylesMap.set(importedModule.url, {
id: importedModule.id ?? importedModule.url,
url: importedModule.url,
content: ssrModule.default,
});
} else {
// NOTE: We use the `url` property here. `id` would break Windows.
importedCssUrls.add(importedModule.url);
}

We then put the CSS module links in links, and the other styles (global styles) in styles.

let links = new Set<SSRElement>();
[...styleUrls].forEach((href) => {
links.add({
props: {
rel: 'stylesheet',
href,
},
children: '',
});
});
let styles = new Set<SSRElement>();
importedStyles.forEach(({ id, url, content }) => {
// Vite handles HMR for styles injected as scripts
scripts.add({
props: {
type: 'module',
src: url,
},
children: '',
});
// But we still want to inject the styles to avoid FOUC. The style tags
// should emulate what Vite injects so further HMR works as expected.
styles.add({
props: {
'data-vite-dev-id': id,
},
children: content,
});
});

Ultimately this creates a tricky situation where we shouldn't actually group head elements this way. Scripts/styles/links should all be in a single array but that takes a larger refactor which we could revisit in the future.


Also, the content rendering runtime also has styles first before links:

unescapeHTML(styles + links + scripts) as any,


In most case, non-CSS links could be sorted anywhere though, and I can confirm CSS links are only for CSS modules, so I think this change is harmless. During builds, all CSS are links so there's no ordering issue.

Testing

Added a new integration test.

Docs

n/a. bug fix.

@changeset-bot
Copy link

changeset-bot bot commented Oct 20, 2023

🦋 Changeset detected

Latest commit: 0f289d3

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pr: docs A PR that includes documentation for review labels Oct 20, 2023
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs approves changeset!

@bluwy bluwy merged commit 26b77b8 into main Oct 23, 2023
13 checks passed
@bluwy bluwy deleted the order-style-before-links branch October 23, 2023 13:22
@astrobot-houston astrobot-houston mentioned this pull request Oct 23, 2023
natemoo-re pushed a commit that referenced this pull request Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pr: docs A PR that includes documentation for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CSS Modules inject order
4 participants