admin管理员组文章数量:1401849
I'm trying to cache a large list of strings(names) on app start so that I don't have to build it everytime I receive an api request to return it.
I tried two ways:
METHOD NO. 1
// my util function to create names
function getDynamicNames() {
return Math.random()+'name';
}
// next.config.ts
export let stars = []
async () => {
const nextConfig = {
// output: 'export', // Outputs a Single-Page Application (SPA)
distDir: 'build', // Changes the build output directory to `build`
}
let i = 0;
while (i < 1000000) {
stars.push(getDynamicNames());
}
return nextConfig;
}
I get an empty array:
// api/test/route.ts
export const GET = () => NextResponse.json({
status: 'success',
message: 'Server is running...',
data: stars
}, {status: 200}) // data -> []
METHOD NO. 2
I get an empty array as well, and yes the register function does run:
// instrumentation.ts
export let stars = []
export async function register() {
let i = 0;
while (i < 1000) {
stars.push(getDynamicNames());
i += 1;
}
}
What is the correct way to cache values on server startup in nextjs
本文标签: nextjscaching values on app start in nextJscannot mutate a variableStack Overflow
版权声明:本文标题:next.js - caching values on app start in nextJs, cannot mutate a variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744323224a2600604.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论