8000 feat: devServer fallback (#1265) · fuse-box/fuse-box@1f5bbe3 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 1f5bbe3

Browse files
Fjandinnchanged
authored andcommitted
feat: devServer fallback (#1265)
1 parent 836ff48 commit 1f5bbe3

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

docs/development/dev-server.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ fuse.dev({
3131
})
3232
```
3333

34+
## Fallback
35+
36+
If you develop a SPA app you can set a fallback. All 404's will return the fallback file you specify. The file path is relative to your served root.
37+
38+
```js
39+
fuse.dev({
40+
root: 'public/build',
41+
fallback: 'index.html'
42+
})
43+
```
44+
3445
## Port
3546
By default you will get `express` application running and a `socket` server (if [HMR](#hot-module-reload) is enabled) bound to port `4444`. To change the port provide `port` option.
3647

src/devServer/HTTPServer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export interface HTTPServerOptions {
1212
/** Provide https server options to enable https */
1313
https?: https.ServerOptions;
1414

15+
/** 404 fallback */
16+
fallback?: string;
17+
1518
/**
1619
* If specfied this is the folder served from express.static
1720
* It can be an absolute path or relative to `appRootPath`
@@ -101,5 +104,10 @@ Development server running ${opts.https ? 'https' : 'http'}://localhost:${port}
101104
this.app.use(this.fuse.context.sourceMapsRoot, express.static(this.fuse.context.homeDir));
102105
}
103106
}
107+
if (this.opts.fallback) {
108+
this.app.use('*', (req, res) => {
109+
res.sendFile(this.opts.fallback)
110+
})
111+
}
104112
}
105113
}

src/devServer/Server.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FuseBox } from "../core/FuseBox";
55
import { utils } from "realm-utils";
66
import * as process from "process";
77
import * as https from 'https';
8+
import * as path from 'path';
89

910
export type HotReloadEmitter = (server: Server, sourceChangedInfo: any) => any;
1011

@@ -22,6 +23,9 @@ export interface ServerOptions {
2223
/** If not specified it uses regular http */
2324
https?: https.ServerOptions;
2425

26+
/** 404 fallback */
27+
fallback?: string;
28+
2529
/**
2630
* - If false nothing is served.
2731
* - If string specified this is the folder served from express.static
@@ -67,6 +71,7 @@ export class Server {
6771
? (utils.isString(opts.root) ? ensureUserPath(opts.root as string) : false) : rootDir;
6872
const port = opts.port || 4444;
6973
const https = opts.https
74+
const fallback = root && opts.fallback && path.join(root, opts.fallback)
7075
if (opts.hmr !== false && this.fuse.context.useCache === true) {
7176

7277
setTimeout(() => {
@@ -85,7 +90,7 @@ export class Server {
8590
if (opts.httpServer === false) {
8691
this.socketServer = SocketServer.startSocketServer(port, this.fuse);
8792
} else {
88-
this.socketServer = this.httpServer.launch({ root, port, https }, opts);
93+
this.socketServer = this.httpServer.launch({ root, port, https, fallback }, opts);
8994
}
9095
});
9196
return this;

src/tests/BundleApi.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ export class BundleApiTest {
111111
should(fuse.producer.devServerOptions).deepEqual({ port: 4444, https: { cert: "cert", key: "key" } })
112112
}
113113

114+
"Should setup devServer with fallback param"() {
115+
const fuse = createFuse();
116+
fuse.dev({ fallback: "index.html" });
117+
should(fuse.producer.devServerOptions).deepEqual({ port: 4444, fallback: "index.html" })
118+
}
119+
114120
"Should inject 1 plugin"() {
115121
const fuse = createFuse();
116122
const bundle = fuse.bundle("app").plugin("first");

0 commit comments

Comments
 (0)
0