@netlify/next-runtime
Version:
Run Next.js seamlessly on Netlify
42 lines (41 loc) • 1.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMockedRequestHandlers = void 0;
const path_1 = require("path");
const promises_1 = __importDefault(require("fs/promises"));
const blobs_1 = require("@netlify/blobs");
// @ts-expect-error no types installed
const fs_monkey_1 = require("fs-monkey");
async function getMockedRequestHandlers(...args) {
const store = (0, blobs_1.getDeployStore)();
const ofs = { ...promises_1.default };
async function readFileFallbackBlobStore(...args) {
const [path, options] = args;
try {
// Attempt to read from the disk
// important to use the `import * as fs from 'fs'` here to not end up in a endless loop
return await ofs.readFile(path, options);
}
catch (error) {
// only try to get .html files from the blob store
if (typeof path === 'string' && path.endsWith('.html')) {
const blobKey = (0, path_1.relative)((0, path_1.join)(process.cwd(), '.next'), path);
const file = await store.get(blobKey);
if (file !== null) {
return file;
}
}
throw error;
}
}
// patch the file system for fs.promises with operations to fallback on the blob store
(0, fs_monkey_1.patchFs)({
readFile: readFileFallbackBlobStore,
}, require('fs').promises);
const { getRequestHandlers } = await import('next/dist/server/lib/start-server.js');
return getRequestHandlers(...args);
}
exports.getMockedRequestHandlers = getMockedRequestHandlers;