Skip to content

Commit

Permalink
Removed <style> with type="text/css" from inline output at build …
Browse files Browse the repository at this point in the history
…time (#8480)
  • Loading branch information
yamanoku authored Sep 12, 2023
1 parent bf341d6 commit 6448258
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-dancers-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'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

0 comments on commit 6448258

Please sign in to comment.