Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): @nrwl/web:file-server throws 404 after refresh #9839

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/generated/packages/web.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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