From 25217a597a5ae6202d74dddf377202791916f568 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 15 Aug 2022 13:04:53 +0100 Subject: [PATCH] chore: retain patched server files --- packages/runtime/src/helpers/files.ts | 12 ++++++++++-- packages/runtime/src/index.ts | 3 +-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/runtime/src/helpers/files.ts b/packages/runtime/src/helpers/files.ts index 1e7971abdf..2629eb6e0c 100644 --- a/packages/runtime/src/helpers/files.ts +++ b/packages/runtime/src/helpers/files.ts @@ -280,6 +280,8 @@ export const moveStaticPages = async ({ } } +const PATCH_WARNING = `/* File patched by Netlify */` + /** * Attempt to patch a source file, preserving a backup */ @@ -294,7 +296,13 @@ const patchFile = async ({ console.warn('File was not found') return false } - const content = await readFile(file, 'utf8') + let content = await readFile(file, 'utf8') + + // If the file has already been patched, patch the backed-up original instead + if (content.includes(PATCH_WARNING) && existsSync(`${file}.orig`)) { + content = await readFile(`${file}.orig`, 'utf8') + } + const newContent = replacements.reduce((acc, [from, to]) => { if (acc.includes(to)) { console.log('Already patched. Skipping.') @@ -307,7 +315,7 @@ const patchFile = async ({ return false } await writeFile(`${file}.orig`, content) - await writeFile(file, newContent) + await writeFile(file, `${newContent}\n${PATCH_WARNING}`) console.log('Done') return true } diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index ad12b0753a..3bb6046158 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -16,7 +16,7 @@ import { generateCustomHeaders, } from './helpers/config' import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge' -import { moveStaticPages, movePublicFiles, patchNextFiles, unpatchNextFiles } from './helpers/files' +import { moveStaticPages, movePublicFiles, patchNextFiles } from './helpers/files' import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions' import { generateRedirects, generateStaticRedirects } from './helpers/redirects' import { shouldSkip, isNextAuthInstalled, getCustomImageResponseHeaders } from './helpers/utils' @@ -200,7 +200,6 @@ const plugin: NetlifyPlugin = { warnForProblematicUserRewrites({ basePath, redirects }) warnForRootRedirects({ appDir }) - await unpatchNextFiles(basePath) }, } module.exports = plugin