Skip to content

Commit

Permalink
chore: include json and js files in prerendered/dependencies folder
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Dec 31, 2024
1 parent 1f89d48 commit 488aa8c
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { resolve } from 'node:path'
import crypto from 'node:crypto'
import fs from 'node:fs'
import type { ResolvedConfig } from 'vite'
import type { VitePWAOptions } from 'vite-plugin-pwa'
import type { ManifestEntry, ManifestTransform } from 'workbox-build'
Expand Down Expand Up @@ -120,9 +118,13 @@ function createManifestTransform(
let url = e.url
// client assets in `.svelte-kit/output/client` folder.
// SSG pages in `.svelte-kit/output/prerendered/pages` folder.
// SPA pages in `.svelte-kit/output/prerendered/dependencies/` folder.
// static adapter with load functions in `.svelte-kit/output/prerendered/dependencies/<page>/_data.json`.
// fallback page in `.svelte-kit/output/prerendered` folder (fallback.html is the default).
if (url.startsWith('client/'))
url = url.slice(7)
else if (url.startsWith('prerendered/dependencies/'))
url = url.slice(25)
else if (url.startsWith('prerendered/pages/'))
url = url.slice(18)
else if (url === defaultAdapterFallback)
Expand Down Expand Up @@ -186,7 +188,7 @@ function createManifestTransform(
function buildGlobPatterns(globPatterns?: string[]) {
if (globPatterns) {
if (!globPatterns.some(g => g.startsWith('prerendered/')))
globPatterns.push('prerendered/**/*.html')
globPatterns.push('prerendered/**/*.{html,json,js}')

if (!globPatterns.some(g => g.startsWith('client/')))
globPatterns.push('client/**/*.{js,css,ico,png,svg,webp,webmanifest}')
Expand All @@ -197,7 +199,7 @@ function buildGlobPatterns(globPatterns?: string[]) {
return globPatterns
}

return ['client/**/*.{js,css,ico,png,svg,webp,webmanifest}', 'prerendered/**/*.html']
return ['client/**/*.{js,css,ico,png,svg,webp,webmanifest}', 'prerendered/**/*.{html,json,js}']
}

function buildGlobIgnores(globIgnores?: string[]) {
Expand All @@ -211,10 +213,15 @@ function buildGlobIgnores(globIgnores?: string[]) {
return ['server/**']
}

function buildManifestEntry(url: string, path: string): Promise<ManifestEntry & { size: number }> {
async function buildManifestEntry(url: string, path: string): Promise<ManifestEntry & { size: number }> {
const [crypto, createReadStream] = await Promise.all([
import('node:crypto').then(m => m.default),
import('node:fs').then(m => m.createReadStream),
])

return new Promise((resolve, reject) => {
const cHash = crypto.createHash('MD5')
const stream = fs.createReadStream(path)
const stream = createReadStream(path)
stream.on('error', (err) => {
reject(err)
})
Expand Down

0 comments on commit 488aa8c

Please sign in to comment.