Skip to content

Commit 6b0b5c1

Browse files
committed
fix: fix hash
1 parent 478f10d commit 6b0b5c1

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ Faster rendering and more readable seo
2222
<!-- Highlight some of the features your module provide here -->
2323

2424
- 🚠 &nbsp;Optimal Caching
25-
-&nbsp;Supports all rendering modes, spa, ssg, ssr and ssr with
26-
pre-rendering.
25+
-&nbsp;Supports ssg, ssr and ssr with pre-rendering.
2726
- 🌲 &nbsp;Intelligent minification extraction, removing unused styles from the
2827
page, merging duplicate styles
2928

README_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
## Features
2020

2121
- 🚠 &nbsp;最佳的缓存
22-
-&nbsp;支持所有渲染模式, spa, ssg, ssr 和带预渲染的 ssr
22+
-&nbsp;支持 ssg, ssr 和带预渲染的 ssr
2323
- 🌲 &nbsp;智能精简提取,移除页面中未使用的样式,合并重复的样式
2424

2525
<br />

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"prepack": "nuxt-module-build build",
2222
"dev": "nuxi dev playground",
2323
"dev:build": "nuxi build playground",
24+
"dev:generate": "nuxi generate playground",
2425
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
2526
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
2627
"lint": "eslint .",

playground/app.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</template>
66

77
<script setup>
8-
useServerHead({
8+
useHead({
99
style: [
1010
{
1111
innerHTML: 'body { color: red }',

playground/nuxt.config.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@ export default defineNuxtConfig({
33
devtools: { enabled: true },
44
compatibilityDate: '2024-08-16',
55
sourcemap: false,
6-
styleExtractor: {
7-
removeUnused: false,
8-
minify: false,
9-
},
106
})

src/module.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { join } from 'node:path'
21
import fs from 'node:fs/promises'
32
import { existsSync } from 'node:fs'
3+
import { join, isAbsolute } from 'node:path'
44
import { hash } from 'ohash'
55
import { addPlugin, addServerPlugin, addTemplate, addTypeTemplate, createResolver, defineNuxtModule } from '@nuxt/kit'
66

@@ -44,6 +44,18 @@ export interface ModuleOptions {
4444
* ```
4545
*/
4646
transformFile: string
47+
48+
/**
49+
* @default 'nuxt-style-extractor'
50+
* @description If you want to invalidate all caches, then change the baseHash
51+
*/
52+
baseHash: string
53+
54+
/**
55+
* @default 'public, max-age=31536000, immutable'
56+
* @description Set cache header, valid only when ssr is in production
57+
*/
58+
cacheControl: string | null
4759
}
4860

4961
export default defineNuxtModule<ModuleOptions>({
@@ -55,6 +67,8 @@ export default defineNuxtModule<ModuleOptions>({
5567
minify: true,
5668
removeUnused: true,
5769
transformFile: '',
70+
baseHash: 'nuxt-style-extractor',
71+
cacheControl: 'public, max-age=31536000, immutable',
5872
},
5973
async setup(_options, nuxt) {
6074
const resolver = createResolver(import.meta.url)
@@ -85,20 +99,20 @@ export default defineNuxtModule<ModuleOptions>({
8599
})
86100
}
87101

102+
const transformFile = getTransformFile()
103+
88104
addTemplate({
89105
filename: 'nuxt-style-extractor-config-hash.js',
90-
getContents() {
91-
return `export const configHash = "${hash(_options)}"`
106+
async getContents() {
107+
const modeText = await fs.readFile(transformFile, 'utf-8')
108+
return `export const configHash = "${hash([_options, modeText])}"`
92109
},
93110
})
94111

95112
addTemplate({
96113
filename: 'nuxt-style-extractor-transform.js',
97114
getContents() {
98-
if (_options.transformFile !== '') {
99-
return fs.readFile(_options.transformFile, 'utf-8')
100-
}
101-
return fs.readFile(getDefaultTransformFile(), 'utf-8')
115+
return fs.readFile(transformFile, 'utf-8')
102116
},
103117
})
104118

@@ -109,7 +123,20 @@ export default defineNuxtModule<ModuleOptions>({
109123
},
110124
})
111125

112-
function getDefaultTransformFile() {
126+
if (_options.cacheControl && !nuxt.options.dev) {
127+
nuxt.options.routeRules ??= {}
128+
nuxt.options.routeRules['/_css/*'] = {
129+
headers: {
130+
'Cache-Control': _options.cacheControl,
131+
},
132+
}
133+
}
134+
135+
function getTransformFile() {
136+
if (_options.transformFile !== '') {
137+
return isAbsolute(_options.transformFile) ? _options.transformFile : join(nuxt.options.rootDir, _options.transformFile)
138+
}
139+
113140
if (_options.minify && _options.removeUnused) {
114141
return resolver.resolve('./runtime/transforms/best.js')
115142
}

0 commit comments

Comments
 (0)