fix(pdfjs): ensure correct MIME type for PDF.js worker files
This commit is contained in:
@@ -64,6 +64,12 @@ server {
|
||||
try_files \$uri \$uri/ ${RAGFLOW_BASE}index.html;
|
||||
}
|
||||
|
||||
# 为 pdfjs worker 提供正确的 Content-Type(避免 .mjs 被当作 application/octet-stream)
|
||||
location ^~ /pdfjs-dist/ {
|
||||
# 统一按 application/javascript 返回,确保浏览器可作为模块脚本加载
|
||||
default_type application/javascript;
|
||||
}
|
||||
|
||||
# 静态资源缓存
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
|
||||
expires 1y;
|
||||
|
||||
28
public/pdfjs-dist/pdf.worker.min.js
vendored
Normal file
28
public/pdfjs-dist/pdf.worker.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -3,12 +3,18 @@ import path from 'node:path';
|
||||
|
||||
const src = path.resolve('node_modules/pdfjs-dist/build/pdf.worker.min.mjs');
|
||||
const destDir = path.resolve('public/pdfjs-dist');
|
||||
const dest = path.join(destDir, 'pdf.worker.min.mjs');
|
||||
const destMjs = path.join(destDir, 'pdf.worker.min.mjs');
|
||||
const destJs = path.join(destDir, 'pdf.worker.min.js');
|
||||
|
||||
try {
|
||||
fs.mkdirSync(destDir, { recursive: true });
|
||||
fs.copyFileSync(src, dest);
|
||||
console.log(`[pdfjs-dist] Worker copied to: ${dest}`);
|
||||
// 复制 .mjs
|
||||
fs.copyFileSync(src, destMjs);
|
||||
console.log(`[pdfjs-dist] Worker copied to: ${destMjs}`);
|
||||
|
||||
// 同步生成 .js 文件以兼容部分服务器缺少 mjs MIME 的情况
|
||||
fs.copyFileSync(src, destJs);
|
||||
console.log(`[pdfjs-dist] Worker duplicated to: ${destJs}`);
|
||||
} catch (err) {
|
||||
console.error('[pdfjs-dist] Failed to copy worker:', err);
|
||||
process.exitCode = 1;
|
||||
|
||||
@@ -2,7 +2,9 @@ import React, { useState, useEffect, useRef } from 'react';
|
||||
// 使用 pdf.js 在左侧容器中渲染 PDF 页面,支持自由滑动与页码定位
|
||||
import * as pdfjsLib from 'pdfjs-dist';
|
||||
|
||||
const PDFJS_WORKER_MJS = '/pdfjs-dist/pdf.worker.min.mjs';
|
||||
// 线上 Nginx 默认不识别 .mjs 的 MIME(会返回 application/octet-stream),
|
||||
// 这里统一使用 .js 扩展以确保 Content-Type 为 application/javascript
|
||||
const PDFJS_WORKER_MJS = '/pdfjs-dist/pdf.worker.min.js';
|
||||
// @ts-ignore
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc = PDFJS_WORKER_MJS;
|
||||
// @ts-ignore
|
||||
|
||||
Reference in New Issue
Block a user