-
Notifications
You must be signed in to change notification settings - Fork 49
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
How does this work with apps with routes? #6
Comments
Since the plugin only does prerendering of a source, the way to go here would be to create an instance of HtmlWebpackPlugin for each route. You can see how In a nutshell, it's roughly: const URLS = ['/', '/a', '/b'];
module.exports = {
// in a webpack config
plugins: [
].concat( URLS.map(url =>
new HtmlWebpackPlugin({
filename: url + '/index.html',
template: '!!prerender-loader?'+encodeURIComponent(JSON.stringify(
string: true,
params: { url }
))+'!index.html'
})
) )
} |
Thanks! So the result would be several HTML files that should be then served somehow by my own http server? |
yup! each with their own independent static HTML (and initial state, titles, etc that you might have injected during prerendering). I just amended the example with a |
Update: you can now also configure JSDOM to report custom URLs for |
This is cool 😎 |
I've tried with multiple
const getUrlPath = (url) => url.match('[^\/]+$')[0]
const prerenderParams = (url) => encodeURIComponent(JSON.stringify({string: true, params: {url}, documentUrl: getUrlPath(url)}))
new HtmlWebpackPlugin({
template: `!!prerender-loader?${prerenderParams(url)}!pug-loader!$./index.html`,
inject: true
excludeChunks: ...
}) |
Hmm - that wouldn't allow for setting parameters since each has a child build. Perhaps the ssr-bundle could be omitted from the parent compiler's assets.. |
For anyone arriving here after I did: I had to make a few additions and changes to the code above to make route rendering work for me (I'm using
// webpack.config.js
const urls = ['/', '/about/'];
webpack.plugins = webpack.plugins.concat(urls.map((url) => {
return new HtmlWebpackPlugin({
template: `!!prerender-loader?${JSON.stringify({string: true, params: {url}})}!${path.join(__dirname, '/src/index.html')}`,
filename: path.join(__dirname, `/dist${url}index.html`),
});
}))
// index.js
import * as ReactDOM from 'react-dom';
import { BrowserRouter, StaticRouter, Route } from 'react-router-dom';
import HomePage from './pages/home';
import AboutPage from './pages/about';
// This part is run in the browser, using Browser Router
ReactDOM.hydrate(
<BrowserRouter>
<React.Fragment>
<Route path='/' exact component={HomePage} />
<Route path='/about/' exact component={AboutPage} />
</React.Fragment>
</BrowserRouter>
, document.getElementById('app')
);
// I had to add the below to get html-webpack-plugin to output the correct markup for each route
// Params from the loader are sent to this function
// Note that this function returns `undefined`
export default (params) => {
ReactDOM.render(
<StaticRouter location={params.url} context={{}}>
<React.Fragment>
<Route path='/' exact component={HomePage} />
<Route path='/about/' exact component={AboutPage} />
</React.Fragment>
</StaticRouter>
, document.getElementById('app')
)
}; Hope this helps! |
@MikaAK were you able to get this working in a Webpack config containing multiple entries? I've run into the same |
@andybflynn Can you give us a walk through what we need to do to emulate your setup for my project? Why do you have a slash at the end of I used the same code for the webpack.config.js:
And this is what my index file looks like (i'm using typescript)
Starting or building the app (and then serving it) doesn't seem to make any difference in how the app works in this config. Package versions:
|
@borisyordanov The only reason I had the forward slash at the end of
instead of
As long as your I didn't have to do anything else to get it working. Adding the default export was the breakthrough moment for me. I'm using the same package versions as you. |
For people who may have the same issue. $npm i
$npm run compile |
How can you use this project to pre-render an app that provides different pages at different routes?
The text was updated successfully, but these errors were encountered: