Skip to content

Commit 63ca05a

Browse files
committed
fix(react): don't generate preload tags for inlined assets
1 parent af1fbff commit 63ca05a

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

packages/fastify-react/plugin/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { closeBundle } from './preload.js'
1212
import { parseStateKeys } from './parsers.js'
1313
import { generateStores } from './stores.js'
1414

15-
export default function viteFastifyVue () {
15+
export default function viteFastifyReactPlugin () {
1616
const context = {
1717
root: null,
1818
}
@@ -23,6 +23,15 @@ export default function viteFastifyVue () {
2323
config,
2424
configResolved: configResolved.bind(context),
2525
resolveId: resolveId.bind(context),
26+
configEnvironment (name, config, { mode }) {
27+
if (mode === 'production') {
28+
config.build.minify = true
29+
config.build.sourcemap = false
30+
}
31+
if (name === 'ssr') {
32+
config.build.manifest = false
33+
}
34+
},
2635
async load (id) {
2736
if (id.includes('?server') && !this.environment.config.build?.ssr) {
2837
const source = loadSource(id)
@@ -51,9 +60,8 @@ export default function viteFastifyVue () {
5160
order: 'post',
5261
handler: transformIndexHtml.bind(context)
5362
},
54-
closeBundle: {
55-
order: 'post',
56-
handler: closeBundle.bind(context),
63+
closeBundle () {
64+
closeBundle.call(this, context.resolvedBundle)
5765
},
5866
}]
5967
}

packages/fastify-react/plugin/preload.js

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,51 @@ import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs'
22
import { join, parse as parsePath } from 'node:path'
33
import { HTMLRewriter } from 'html-rewriter-wasm'
44

5-
const imageFile = /\.((png)|(jpg)|(svg)|(webp)|(gif))$/
5+
const imageFileRE = /\.((png)|(jpg)|(svg)|(webp)|(gif))$/
66

7-
export async function closeBundle() {
8-
if (!this.resolvedConfig.build.ssr) {
9-
const distDir = join(this.root, this.resolvedConfig.build.outDir)
10-
const indexHtml = readFileSync(join(distDir, 'index.html'), 'utf8')
11-
const pages = Object.fromEntries(
12-
Object.entries(this.resolvedBundle ?? {})
13-
.filter(([id, meta]) => {
14-
if (meta.facadeModuleId?.includes('/pages/')) {
15-
meta.htmlPath = meta.facadeModuleId.replace(/.*pages\/(.*)\.jsx$/, 'html/$1.html')
16-
return true
17-
}
18-
})
19-
)
20-
for (const page of Object.values(pages)) {
21-
const jsImports = page.imports
22-
const cssImports = page.viteMetadata.importedCss
23-
const images = page.moduleIds.filter(_ => imageFile.test(_))
24-
let imagePreloads = '\n'
25-
for (let image of images) {
26-
image = image.slice(this.root.length + 1)
27-
imagePreloads += ` <link rel="preload" as="image" crossorigin href="${this.resolvedConfig.base}${image}">\n`
28-
}
29-
let cssPreloads = ''
30-
for (const css of cssImports) {
31-
cssPreloads += ` <link rel="preload" as="style" crossorigin href="${this.resolvedConfig.base}${css}">\n`
32-
}
33-
let jsPreloads = ''
34-
for (const js of jsImports) {
35-
jsPreloads += ` <link rel="modulepreload" crossorigin href="${this.resolvedConfig.base}${js}">\n`
36-
}
37-
const pageHtml = await appendHead(
38-
indexHtml,
39-
imagePreloads,
40-
cssPreloads,
41-
jsPreloads
42-
)
43-
writeHtml(page, pageHtml, distDir)
7+
export async function closeBundle(resolvedBundle) {
8+
if (this.environment.name !== 'client') {
9+
return
10+
}
11+
const { assetsInlineLimit } = this.environment.config.build
12+
const { root, base } = this.environment.config
13+
const distDir = join(root, this.environment.config.build.outDir)
14+
const indexHtml = readFileSync(join(distDir, 'index.html'), 'utf8')
15+
const pages = Object.fromEntries(
16+
Object.entries(resolvedBundle ?? {})
17+
.filter(([id, meta]) => {
18+
if (meta.facadeModuleId?.includes('/pages/')) {
19+
meta.htmlPath = meta.facadeModuleId.replace(/.*pages\/(.*)\.jsx$/, 'html/$1.html')
20+
return true
21+
}
22+
})
23+
)
24+
for (const page of Object.values(pages)) {
25+
const jsImports = page.imports
26+
const cssImports = page.viteMetadata.importedCss
27+
const images = page.moduleIds.filter((img) => {
28+
return (page.modules[img].originalLength > assetsInlineLimit) && imageFileRE.test(img)
29+
})
30+
let imagePreloads = '\n'
31+
for (let image of images) {
32+
image = image.slice(root.length + 1)
33+
imagePreloads += ` <link rel="preload" as="image" crossorigin href="${base}${image}">\n`
34+
}
35+
let cssPreloads = ''
36+
for (const css of cssImports) {
37+
cssPreloads += ` <link rel="preload" as="style" crossorigin href="${base}${css}">\n`
4438
}
39+
let jsPreloads = ''
40+
for (const js of jsImports) {
41+
jsPreloads += ` <link rel="modulepreload" crossorigin href="${base}${js}">\n`
42+
}
43+
const pageHtml = await appendHead(
44+
indexHtml,
45+
imagePreloads,
46+
cssPreloads,
47+
jsPreloads
48+
)
49+
writeHtml(page, pageHtml, distDir)
4550
}
4651
}
4752

0 commit comments

Comments
 (0)