就像你说的,你可以使用 react-static。
它们具有完全满足您需求的功能(用户的特定页面)。
在他们的示例中,他们使用一组帖子为每个帖子生成一个特定的静态文件。
加载时间要少得多,因为它只是 html 静态文件。
想象一下有这样的场景:
[
{
id: 'foo',
...
},
{
id: 'bar',
...
},
...
]
按照下面的示例,这将生成类似这样的内容(在运行时):
- src
- pages
- blog
- posts
- foo // Specific post page
- bar // Specific post page
看例子:
//static.config.js
export default {
// resolves an array of route objects
getRoutes: async () => {
// this is where you can make requests for data that will be needed for all
// routes or multiple routes - values returned can then be reused in route objects below
// ATTENTION: In here, instead of posts you'd fetch your users json data
const { data: posts } = await axios.get(
"https://jsonplaceholder.typicode.com/posts"
);
return [
// route object
{
// React Static looks for files in src/pages (see plugins below) and matches them to path
path: "/blog",
// function that returns data for this specific route
getData: () => ({
posts
}),
// an array of children routes
// in this case we are mapping through the blog posts from the post variable above
// and setting a custom route for each one based off their post id
children: posts.map(post => ({
path: `/post/${post.id}`,
// location of template for child route
template: "src/containers/Post",
// passing the individual post data needed
getData: () => ({
post
})
}))
},
];
},
// basic template default plugins
plugins: [
[
require.resolve("react-static-plugin-source-filesystem"),
{
location: path.resolve("./src/pages")
}
],
require.resolve("react-static-plugin-reach-router"),
require.resolve("react-static-plugin-sitemap")
]
};