-
Notifications
You must be signed in to change notification settings - Fork 24
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
Dynamic Routes #2
Comments
Update Thinking out loud here. One possible approach is webpack contexts. For example, given a // Import all necessary modules up here...
// Get a list of post components using webpack context
const context = require.context('./posts', false, /\.js$/);
const posts = context.keys().sort().map(context);
export const routes = (
<Route path='/' title='App' component={App}>
{/* Other router config here... */}
{posts.map((module, i) => {
const Component = module.__esModule ? module.default : module;
return <Route key={i} path={String(i)} component={Component} />;
})}
<Route path='*' title='404: Post Not Found' component={NotFound} />
</Route>
);
export default routes; |
Another potential solution would be to expect directories to exist based on routes. For example, given the following router config: // Import all necessary modules up here...
export const routes = (
<Route path='/' title='App' component={App}>
<Route path='posts' component={Posts}>
<IndexRoute component={PostList} />
<Route path=':id' component={Post} />
</Route>
<Route path='*' title='404: Post Not Found' component={NotFound} />
</Route>
);
export default routes; We would see the Drawbacks:
|
Why not allow to pass path ([]) as option and then in That would remove any dependencies on file system or such and open it for other underlying logic to generate the paths.
Or are there some pitfalls that I do not see? |
Hm, where would you get the I'm not entirely clear how this would work, but you might be on to something here. So let's assume you already had the array of paths, how would use use them in your If you put together or a complete example of how the API might look I'm happy to check it out. I would also welcome a pull request if you think you have a solution :) |
@iansinnott the path you can inject or write them as I did in the above example. I will have a look now since I am may need to change how the plugin resolves the paths. As short explanation I am ATM working on https://github.com/scherler/react-static-boilerplate/tree/redux the redux branch to add it to the boilerplate if you like it. However there is a problem in the way we normally would invoke the store vs. the export all routes. If you look into the above branch you will find
To inject the store you need to to this in the top parent or you will need to do it in each route. I am still playing around to see whether I can move it around somehow, will let you know. |
@iansinnott It uses https://github.com/markdalgleish/static-site-generator-webpack-plugin/ underneath which provides a However, I like your |
Hey @sidjain26 thanks for the ping. I haven't looked at this issue in a while, because for whatever reason I haven't yet run into the need for dynamic routes. I've been using this plugin extensively but it's all been for sites without dynamic routing. In my own minimal testing in the past the context approach actually worked quite well. it's currently the option I favor because it is explicit and doesn't require the user to know any special configuration—this is a first class goal of this project. What's also nice is that the Once I rewrite my blog using this plugin I will definitely figure out how to do dynamic routing. |
@iansinnott how would you do something like this for I18n routes? e.g. /about and /fr/about? |
@lifeiscontent I'm not sure, but my guess is that Webpack has a i18n solution. Have you tried internationalizing a web app with webpack yet? |
AKA routes that look like
The issue
We need dynamic routes to support creating an entire category of sties including blogs. However currently we don't have a way to turn something like
posts/:id
into a file directly. Normally we would have something like this:Which would generate the following HTML...
But in our case React Router has no way of knowing the contents of our
posts/
directory so how would it know what files to generate?Most static site generators would read the
posts/
directory and then generate static sites from those files. However, that requires the user to either learn how to configure their generator or learn the conventions for creating dynamic content.We could do the same thing:
But then the user would have to know how to hook into this process. It is a first-class goal of this project to not force the user to learn some new technology. Just add this plugin to a webpack config and go. This is the issue I'd like to solve.
The text was updated successfully, but these errors were encountered: