Merge pull request #195 from shumaiOne/feat/pdf-worker-route

feat(web): register pdf.worker route in production
This commit is contained in:
Yiling-J
2026-07-18 19:44:43 +08:00
committed by GitHub
2 changed files with 31 additions and 5 deletions
+23 -1
View File
@@ -1,5 +1,6 @@
import { loadEnvConfig } from '@shumai/core/src/env-loader'
import { join } from 'node:path'
import { readdirSync } from 'node:fs'
import { dirname, join } from 'node:path'
loadEnvConfig(process.cwd(), process.env.NODE_ENV !== 'production')
import index from '@shumai/webui/index.html'
@@ -84,6 +85,27 @@ async function run() {
routes[routePath] = () => new Response(Bun.file(filePath), { headers: file.headers })
}
const sampleFile = bundle.files[0]?.path || bundle.index || ''
const bundleDir = join(import.meta.dir, dirname(sampleFile))
try {
const dirFiles = readdirSync(bundleDir)
const pdfWorkerFile = dirFiles.find((f) => f.startsWith('pdf.worker'))
if (pdfWorkerFile) {
const routePath = '/' + pdfWorkerFile
const filePath = join(bundleDir, pdfWorkerFile)
const bunFile = Bun.file(filePath)
routes[routePath] = () =>
new Response(bunFile, {
headers: {
'content-type': 'text/javascript;charset=utf-8',
etag: `"${bunFile.size}-${bunFile.lastModified}"`,
},
})
}
} catch (err) {
console.warn('Failed to register pdf.worker route:', err)
}
const mainHtmlPath = join(import.meta.dir, bundle.index)
const mainHtmlFile = Bun.file(mainHtmlPath)
const mainHtmlHeaders = bundle.files.find((f) => f.path === bundle.index)?.headers || {
@@ -6,12 +6,16 @@ import React, { useCallback, useEffect, useImperativeHandle, useRef, useState }
import { FileViewerProps, MediaController } from '../types'
import { PdfControlBar } from './pdf-control-bar'
const { default: pdfworker } = await import('@/ui/public/pdf.worker.min.mjs', {
with: { type: 'file' },
})
import pdfworker from '@/ui/public/pdf.worker.min.mjs' with { type: 'file' }
if (typeof window !== 'undefined' && pdfjsLib.GlobalWorkerOptions) {
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfworker
const workerPath =
typeof pdfworker === 'string'
? pdfworker.startsWith('/')
? pdfworker
: '/' + pdfworker.replace(/^\.\//, '')
: pdfworker
pdfjsLib.GlobalWorkerOptions.workerSrc = workerPath
}
export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(