Skip to content

Commit

Permalink
fix(web): @nrwl/web:file-server throws 404 after refresh (#9839)
Browse files Browse the repository at this point in the history
  • Loading branch information
imhuytq committed Jun 10, 2022
1 parent 3b3888a commit 3de1cb6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
5 changes: 5 additions & 0 deletions docs/generated/packages/web.json
Expand Up @@ -937,6 +937,11 @@
"type": "boolean",
"description": "Watch for file changes.",
"default": true
},
"spa": {
"type": "boolean",
"description": "Redirect 404 errors to index.html (useful for SPA's)",
"default": false
}
},
"additionalProperties": false,
Expand Down
28 changes: 22 additions & 6 deletions packages/web/src/executors/file-server/file-server.impl.ts
@@ -1,15 +1,18 @@
import {
ExecutorContext,
joinPathFragments,
workspaceLayout,
} from '@nrwl/devkit';
import * as chalk from 'chalk';
import { execFileSync, fork } from 'child_process';
import { watch } from 'chokidar';
import { copyFileSync, unlinkSync } from 'fs';
import { createIgnore } from 'nx/src/utils/ignore';
import { readModulePackageJson } from 'nx/src/utils/package-json';
import { platform } from 'os';
import { resolve } from 'path';
import { join, resolve } from 'path';

import {
ExecutorContext,
joinPathFragments,
workspaceLayout,
} from '@nrwl/devkit';

import { Schema } from './schema';

// platform specific command name
Expand Down Expand Up @@ -139,6 +142,15 @@ export default async function* fileServerExecutor(
run();

const outputPath = getBuildTargetOutputPath(options, context);

if (options.spa) {
const src = join(outputPath, 'index.html');
const dst = join(outputPath, '404.html');

// See: https://github.com/http-party/http-server#magic-files
copyFileSync(src, dst);
}

const args = getHttpServerArgs(options);

const { path: pathToHttpServerPkgJson, packageJson } =
Expand All @@ -163,6 +175,10 @@ export default async function* fileServerExecutor(
if (disposeWatch) {
disposeWatch();
}

if (options.spa) {
unlinkSync(join(outputPath, '404.html'));
}
};
process.on('exit', processExitListener);
process.on('SIGTERM', processExitListener);
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/executors/file-server/schema.d.ts
Expand Up @@ -11,4 +11,5 @@ export interface Schema {
withDeps: boolean;
proxyOptions?: object;
watch?: boolean;
spa: boolean;
}
5 changes: 5 additions & 0 deletions packages/web/src/executors/file-server/schema.json
Expand Up @@ -60,6 +60,11 @@
"type": "boolean",
"description": "Watch for file changes.",
"default": true
},
"spa": {
"type": "boolean",
"description": "Redirect 404 errors to index.html (useful for SPA's)",
"default": false
}
},
"additionalProperties": false,
Expand Down

1 comment on commit 3de1cb6

@vercel
Copy link

@vercel vercel bot commented on 3de1cb6 Jun 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.