Skip to content

Commit 50b6211

Browse files
committed
fix: fix cacheControl
1 parent 0987650 commit 50b6211

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/module.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ export default defineNuxtModule<ModuleOptions>({
105105
nuxt.options.nitro.virtual['#style-extractor/nuxt-style-extractor-transform.js'] = () => {
106106
return fs.readFile(transformFile, 'utf-8')
107107
}
108+
109+
nuxt.options.nitro.virtual['#style-extractor/nuxt-style-extractor-cache-control.js'] = () => {
110+
return `const cacheControl = "${_options.cacheControl === null ? '' : _options.cacheControl}";
111+
export default cacheControl`
112+
}
113+
108114
addPlugin(resolver.resolve(`./runtime/plugin.server`))
109115

110116
addServerPlugin(resolver.resolve(`./runtime/server/plugins/style-extractor`))
@@ -131,15 +137,6 @@ export default defineNuxtModule<ModuleOptions>({
131137
},
132138
})
133139

134-
if (_options.cacheControl && !nuxt.options.dev) {
135-
nuxt.options.routeRules ??= {}
136-
nuxt.options.routeRules['/_css/*'] = {
137-
headers: {
138-
'Cache-Control': _options.cacheControl,
139-
},
140-
}
141-
}
142-
143140
function getTransformFile() {
144141
if (_options.transformFile !== '') {
145142
return isAbsolute(_options.transformFile) ? _options.transformFile : join(nuxt.options.rootDir, _options.transformFile)

src/runtime/nuxt-style-extractor.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ declare module '#style-extractor/nuxt-style-extractor-transform.js' {
2323
const optimiseCss: OptimiseCss
2424
export default optimiseCss
2525
}
26+
27+
declare module '#style-extractor/nuxt-style-extractor-cache-control.js' {
28+
const cacheControl: string
29+
export default cacheControl
30+
}

src/runtime/server/plugins/style-extractor.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineNitroPlugin, defineEventHandler, useStorage, getRouterParam, setHeader } from '#imports'
22
import optimiseCss from '#style-extractor/nuxt-style-extractor-transform.js'
3+
import cacheControl from '#style-extractor/nuxt-style-extractor-cache-control.js'
34

45
export default defineNitroPlugin((nitroApp) => {
56
const nameReg = /data-style-extractor-name="(.*?)"/
@@ -15,6 +16,10 @@ export default defineNitroPlugin((nitroApp) => {
1516
css = await assetsStorage.getItem(name!)
1617
}
1718

19+
if (!import.meta.dev && cacheControl !== '') {
20+
setHeader(event, 'Cache-Control', cacheControl)
21+
}
22+
1823
return css
1924
}))
2025

0 commit comments

Comments
 (0)