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

Removed <style> with type="text/css" from inline output at build time #8480

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/gorgeous-dancers-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astrojs/deno': patch
'@astrojs/node': patch
bluwy marked this conversation as resolved.
Show resolved Hide resolved
'astro': patch
---

Do not add type="text/css" to inline style tag
4 changes: 1 addition & 3 deletions packages/astro/src/core/render/ssr-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export function createStylesheetElement(
): SSRElement {
if (stylesheet.type === 'inline') {
return {
props: {
type: 'text/css',
},
props: {},
children: stylesheet.content,
};
} else {
Expand Down
5 changes: 2 additions & 3 deletions packages/astro/src/runtime/client/hmr.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// <reference types="vite/client" />

if (import.meta.hot) {
// Vite injects `<style type="text/css" data-vite-dev-id>` for ESM imports of styles
// but Astro also SSRs with `<style type="text/css" data-astro-dev-id>` blocks. This MutationObserver
// Vite injects `<style data-vite-dev-id>` for ESM imports of styles
// but Astro also SSRs with `<style data-astro-dev-id>` blocks. This MutationObserver
// removes any duplicates as soon as they are hydrated client-side.
const injectedStyles = getInjectedStyles();
const mo = new MutationObserver((records) => {
Expand Down Expand Up @@ -44,7 +44,6 @@ function isStyle(node: Node): node is HTMLStyleElement {
function isViteInjectedStyle(node: Node): node is HTMLStyleElement {
return (
isStyle(node) &&
node.getAttribute('type') === 'text/css' &&
!!node.getAttribute('data-vite-dev-id')
);
}
2 changes: 1 addition & 1 deletion packages/astro/src/runtime/server/render/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export function renderUniqueStylesheet(result: SSRResult, sheet: StylesheetAsset

if (sheet.type === 'inline') {
if (Array.from(result.styles).some((s) => s.children.includes(sheet.content))) return '';
return renderElement('style', { props: { type: 'text/css' }, children: sheet.content });
return renderElement('style', { props: {}, children: sheet.content });
}
}
1 change: 0 additions & 1 deletion packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ async function getScriptsAndStyles({ pipeline, filePath }: GetScriptsAndStylesPa
// But we still want to inject the styles to avoid FOUC
styles.add({
props: {
type: 'text/css',
// Track the ID so we can match it to Vite's injected style later
'data-astro-dev-id': viteID(new URL(`.${url}`, settings.config.root)),
},
Expand Down
2 changes: 0 additions & 2 deletions packages/integrations/deno/test/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ Deno.test({
const doc = new DOMParser().parseFromString(html, `text/html`);
const style = doc!.querySelector('style');

assertEquals(style?.getAttribute('type'), 'text/css');

assert(style?.textContent?.includes('Courier New'));
});

Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/node/test/prerender-404-500.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('Prerender 404', () => {
const $ = cheerio.load(html);

// length will be 0 if the stylesheet does not get included
expect($('style[type="text/css"]')).to.have.a.lengthOf(1);
expect($('style')).to.have.a.lengthOf(1);
});
});

Expand Down
Loading