From c867b0ce9ce4a42896b6362c6ec43630e398de0a Mon Sep 17 00:00:00 2001 From: Oscar Busk Date: Mon, 10 Dec 2018 12:14:29 +0100 Subject: [PATCH] Fix paths when built on windows (#5795) This PR Fixes #4920 So the problem is that when a next.js application is built on windows, the `pages-manifest.json` file is created with backslashes. If this built application is deployed to a linux hosting enviroment, the server will fail when trying to load the modules. ``` Error: Cannot find module '/user_code/next/server/bundles\pages\index.js ``` My simple solution is to modify the `pages-manifest.json` to always use linux separator (`/`), then also modify `server/require.js` to, when requiring page, replace any separator (`\` or `/`) with current platform-specific file separator (`require('path').sep`). The fix in `server/require.js` would be sufficient, but my opinion is that having some cross-platform consistency is nice. This change was tested by bulding an application in windows and running it in linux and windows, aswell as building an application in linux and running it in linux and windows. The related tests was also run. --- build/webpack/plugins/pages-manifest-plugin.js | 3 ++- test/integration/production/test/index.test.js | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/build/webpack/plugins/pages-manifest-plugin.js b/build/webpack/plugins/pages-manifest-plugin.js index ff58063729..7713d78364 100644 --- a/build/webpack/plugins/pages-manifest-plugin.js +++ b/build/webpack/plugins/pages-manifest-plugin.js @@ -24,7 +24,8 @@ export default class PagesManifestPlugin { } const {name} = entry - pages[`/${pagePath.replace(/\\/g, '/')}`] = name + // Write filename, replace any backslashes in path (on windows) with forwardslashes for cross-platform consistency. + pages[`/${pagePath.replace(/\\/g, '/')}`] = name.replace(/\\/g, '/') } if (typeof pages['/index'] !== 'undefined') { diff --git a/test/integration/production/test/index.test.js b/test/integration/production/test/index.test.js index 1b78a0d0b9..0aca721514 100644 --- a/test/integration/production/test/index.test.js +++ b/test/integration/production/test/index.test.js @@ -16,7 +16,7 @@ import fetch from 'node-fetch' import dynamicImportTests from './dynamic' import processEnv from './process-env' import security from './security' -import {BUILD_MANIFEST, REACT_LOADABLE_MANIFEST} from 'next/constants' +import {BUILD_MANIFEST, REACT_LOADABLE_MANIFEST, PAGES_MANIFEST} from 'next/constants' const appDir = join(__dirname, '../') let appPort @@ -276,6 +276,17 @@ describe('Production Usage', () => { expect(serverSideJsBody).toMatch(/404/) }) + it('should not put backslashes in pages-manifest.json', () => { + // Whatever platform you build on, pages-manifest.json should use forward slash (/) + // See: https://github.com/zeit/next.js/issues/4920 + const pagesManifest = require(join('..', '.next', 'server', PAGES_MANIFEST)) + + for (let key of Object.keys(pagesManifest)) { + expect(key).not.toMatch(/\\/) + expect(pagesManifest[key]).not.toMatch(/\\/) + } + }) + dynamicImportTests(context, (p, q) => renderViaHTTP(context.appPort, p, q)) processEnv(context) -- GitLab