Child routes 404 when application is served statically using web server #5501
-
I would like to use Modern.js to produce a static bundle which can be served as an SPA, without the need to use import { appTools, defineConfig } from '@modern-js/app-tools';
// https://modernjs.dev/en/configure/app/usage
export default defineConfig({
output: {
distPath: {
html: '',
},
cleanDistPath: true,
disableSourceMap: true
},
html: {
disableHtmlFolder: true,
},
source: {
mainEntryName: 'index'
},
runtime: {
router: {
supportHtml5History: true,
}
},
plugins: [
appTools({
bundler: 'webpack',
}),
],
}); And, as a result, my build output looks like this:
I can then serve this using The app has a few routes defined:
Navigating to Going off of the Self-controlled routes documentation, does the Conventional routing require the use of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You need to set up a "rewrite"-like feature based on your deployment platform. for instance, for nginx // https://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
try_files $uri $uri/ /index.html; for azure, create a file named {
"navigationFallback": {
"rewrite": "/index.html"
}
} |
Beta Was this translation helpful? Give feedback.
-
Here are some notes on a working solution. To serve the SPA as a static website, (1) you'll need to configure Modern.js to produce a build directory that looks like the one below, and (2) a proper web server configuration.
This is the minimum export default defineConfig({
output: {
cleanDistPath: true,
distPath: {
html: '',
},
},
html: {
disableHtmlFolder: true,
},
runtime: {
router: true,
},
source: {
mainEntryName: 'index',
},
plugins: [
appTools({
bundler: 'webpack',
}),
],
}); The key points are the (2) This isn't specific to Modern.js, but it's helpful nonetheless, for the web server configuration, you'll need to redirect all requests back to the index of the SPA. This is to get client side routing to work. In our case, we're hosting the application on S3 and serving from CloudFront. To get redirecting to work, an "error page" must be set on CloudFront with a path pointing to the website's |
Beta Was this translation helpful? Give feedback.
Here are some notes on a working solution. To serve the SPA as a static website, (1) you'll need to configure Modern.js to produce a build directory that looks like the one below, and (2) a proper web server configuration.
This is the minimum
modern.config.ts
needed to achieve this: