Skip to content

Commit

Permalink
fix(nuxt): in dev, don't link css files with ?inline query (#25822)
Browse files Browse the repository at this point in the history
  • Loading branch information
OnlyWick committed Feb 16, 2024
1 parent 06352e3 commit e817655
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/nuxt/src/core/runtime/nitro/renderer.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -392,6 +392,14 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse
const link = []
for (const style in styles) {
const resource = styles[style]
// Do not add links to resources that are inlined (vite v5+)
if (import.meta.dev && 'inline' in getURLQuery(resource.file)) {
continue
}
// Add CSS links in <head> 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) })
}
Expand Down
9 changes: 9 additions & 0 deletions test/basic.test.ts
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/basic/assets/inline-only.css
@@ -0,0 +1,3 @@
:root {
--inline-only: 'inline-only';
}
8 changes: 8 additions & 0 deletions test/fixtures/basic/pages/inline-only-css.vue
@@ -0,0 +1,8 @@
<script setup lang="ts">
import css from '~/assets/inline-only.css?inline'
</script>

<template>
<pre>{{ css }}</pre>
</template>

0 comments on commit e817655

Please sign in to comment.