diff --git a/packages/nuxt/src/core/runtime/nitro/renderer.ts b/packages/nuxt/src/core/runtime/nitro/renderer.ts index abcaddbea3..5fb732d0ab 100644 --- a/packages/nuxt/src/core/runtime/nitro/renderer.ts +++ b/packages/nuxt/src/core/runtime/nitro/renderer.ts @@ -13,7 +13,7 @@ import { appendResponseHeader, createError, getQuery, getResponseStatus, getResp import devalue from '@nuxt/devalue' import { stringify, uneval } from 'devalue' import destr from 'destr' -import { joinURL, withoutTrailingSlash } from 'ufo' +import { getQuery as getURLQuery, joinURL, withoutTrailingSlash } from 'ufo' import { renderToString as _renderToString } from 'vue/server-renderer' import { hash } from 'ohash' import { renderSSRHead } from '@unhead/ssr' @@ -392,6 +392,14 @@ export default defineRenderHandler(async (event): Promise for CSS files + // - in production + // - in dev mode when not rendering an island + // - in dev mode when rendering an island and the file has scoped styles and is not a page if (!import.meta.dev || !isRenderingIsland || (resource.file.includes('scoped') && !resource.file.includes('pages/'))) { link.push({ rel: 'stylesheet', href: renderer.rendererContext.buildAssetsURL(resource.file) }) } diff --git a/test/basic.test.ts b/test/basic.test.ts index 42e0b86fe0..cc044801c7 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -1414,6 +1414,15 @@ describe('automatically keyed composables', () => { }) }) +describe.runIf(isDev() && !isWebpack)('css links', () => { + it('should not inject links to CSS files that are inlined', async () => { + const html = await $fetch('/inline-only-css') + expect(html).toContain('--inline-only') + expect(html).not.toContain('inline-only.css') + expect(html).toContain('assets/plugin.css') + }) +}) + describe.skipIf(isDev() || isWebpack)('inlining component styles', () => { const inlinedCSS = [ '{--plugin:"plugin"}', // CSS imported ambiently in JS/TS diff --git a/test/fixtures/basic/assets/inline-only.css b/test/fixtures/basic/assets/inline-only.css new file mode 100644 index 0000000000..0e377e929e --- /dev/null +++ b/test/fixtures/basic/assets/inline-only.css @@ -0,0 +1,3 @@ +:root { + --inline-only: 'inline-only'; +} diff --git a/test/fixtures/basic/pages/inline-only-css.vue b/test/fixtures/basic/pages/inline-only-css.vue new file mode 100644 index 0000000000..44f8a78700 --- /dev/null +++ b/test/fixtures/basic/pages/inline-only-css.vue @@ -0,0 +1,8 @@ + + + +