Replies: 37 comments 32 replies
-
Why don't you simply modify |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
If your app directory component is marked as a client component, using the
But I think I am confused about your use case. Can you clarify your first statement?
Opting out of static page generation causes you to lose ISR because there is nothing to statically generate, hence Incremental Static Regeneration. The only option when opting out of a build runtime is to fetch it dynamically server side (e.g. Please let me know if I misunderstood your problem. |
Beta Was this translation helpful? Give feedback.
-
I will try to explain it via example. Given a page named /about . This page uses data fetched from backend. The backend data will change rarely or never change at all. My problem is that the backend server cannot be reached at build time. Also I want to use the same application with different backend server. So right now in order to build the application I am forced to use dynamic render (SSR). The problem with SSR is that every request to the /about page will dynamically fetch the data (which "never" changes ) and render the html. Of course I can make a custom cache that store the fetched data, but still the html will render every request. Also there is no cache control headers because it's SSR, so every request will hit backend even if it is still the same result every time. So if we generate a static html on demand (not in build time) the nextjs features like cache headers control and ISR still works without the need to create a custom server. |
Beta Was this translation helpful? Give feedback.
-
If we're explicitly saying "use client" at the top of the file, can't this disable the server-side stuff. Annoying to have errors about "localStorage" not available because a client component is trying to render on the server initially for some reason. |
Beta Was this translation helpful? Give feedback.
-
There should be a “use client only” |
Beta Was this translation helpful? Give feedback.
-
@hlege You simply want to avoid data fetching during the build process due to backend resources potentially being unavailable, but you want it to fetch and render statically with ISR once deployed, correct? If this is the case, I think you could simply avoid fetching the data within your server component by referencing a build time constant. async function getPosts() {
const res = await fetch(`https://.../posts`, { next: { revalidate: 60 } });
const data = await res.json();
return data.posts;
}
export default async function PostList() {
let posts = [];
if (process.env.BUILD_ENVIRONMENT !== 'local') {
posts = await getPosts();
}
return posts.map((post) => <div>{post.name}</div>);
}; In the example, |
Beta Was this translation helpful? Give feedback.
-
Your solution is like a hack around a limitation of the framework. And what @Xevion suggested applies only to the dynamic segments and even then it does not work (maybe a bug in nextjs13): if you have a dynamic segment page say you have a folders like this: "/posts/[slug]" and inside there is a page.tsx just like in their documentation
When I try to build this with But what the topic starter and I actually want is the ability to have a cache control with "generate on first request" (if I understood the request correctly). Imagine you have a page that is not generated at build time but set to SSR with a cache control that says "revalidate = false"; basically once rendered it stays cached ( as if it was SSG). This is what ideally would be the most suitable solution for the author of this ticket. |
Beta Was this translation helpful? Give feedback.
-
Yes, I am aware. I called both of those points out specifically in my original comment. My intent was to provide some solution to an uncommon use-case.
I don't think a blog is the best example for the author's original scenario. The author has a limited access to the resources required to generate the pages at build time. I don't think this issue was about avoiding the cost of generating content at build time. It's typically ideal to generate these at build time so that the first user to visit the page doesn't incur the cost of waiting for the page to generate using SSR. This is also what enables the effectiveness ISR — the worst-case performance scenario is the user views stale content as opposed to waiting for a page generated using SSR. |
Beta Was this translation helpful? Give feedback.
-
i am well aware that i can use build time env for that, but than everything need to be checked and its a little "ugly" and i thinks nextjs can support it out of box. Also i think a common cms/portal like webpage is use a render strategy like: render when user request a page -> then cache it until its expired or content is changed.
In nextjs 13 the streaming rendering already solve this problem with a proper page layout structure too. |
Beta Was this translation helpful? Give feedback.
-
@hlege Totally agreed. I realize in retrospect that my eagerness to help you find a near-term solution, albeit a hack, was seemingly dismissive of your original feature request. My apologies for that. To be clear, I do agree something like this would be useful for the framework to expose — in particular the per page configuration at a minimum. Tangentially, I found an issue specific to client components when trying to leverage the Of course, that example is not applicable to your issue since that's specific to client components and client-side fetching. I simply wanted to provide that to present another use-case to support this feature request. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
There are some good points here. I think it's important to keep in mind that SSR can be negatively impacted if the API you're integrating with is slow. For example, I have some personal projects where I serve content from my Notion via their API. In my experience, it's been slower than most so I try to do everything at build time. 😅 Having said that, you're correct in that it's up to the developer to determine those scenarios and edge runtimes can help this tremendously. And as @hlege pointed out, the I think this feature could be an overall win for the framework. |
Beta Was this translation helpful? Give feedback.
-
We have a similar requirement at my organization. Our application has whitelabeling requirements (eg: custom brand name in browser title bar). We need to deploy run multiple versions of the same exact application build multiple times but with slightly different environment variables. The application needs to be deployed via Docker to custom infrastructure (customer requirements, not ours). We want to avoid performing multiple docker builds and maintaining many redundant docker images just to change a few build-time environment variables. Our proposed solution is to use runtime environment variables. We are able to accomplish runtime environment variables with publicRuntimeConfig. I want to use getStaticProps so we can take advantage of incremental static regeneration and not take the performance hit that SSR would cause. Unfortunately there is no means to perform something similar to I don't even need to revalidate as we don't have any blocking data requirements. For all I care I would set revalidate to an arbitrarily high value so that revalidation never happens. Ideally there would be a way to tell the server to generate and cache ALL pages when the built server process starts (similar to what happens with static optimization during build time). Basically what i'm looking for is "run-time" static page optimization instead of "build-time" static page optimization. We have a very small amount of pages, so run-time static optimization slowness isn't a concern. If this isn't currently possible, does the app directory / Suspense make this possible? |
Beta Was this translation helpful? Give feedback.
-
I have the same problem with @agusterodin, which is variables aren’t available at build time. At the moment I fixed it with deleting generated static file after build. RUN yarn build
# Remove SSG file at build time
# Also removes files in .next/standalone/.next folder if next config output is 'standalone'
RUN rm ./.next/**/*.html ./.next/standalone/.next/**/*.html I hope this helps the others. But options to skip SSG at build time is nice to have. |
Beta Was this translation helpful? Give feedback.
-
Is it required to run |
Beta Was this translation helpful? Give feedback.
-
What should be the commands order while using output mode as standalone? Assume a multi stage docker image build. Is it possible to run Overall, how should this process look like, or maybe it is possible to skip |
Beta Was this translation helpful? Give feedback.
-
This isn't a direct solution, but does work for our use-case. We have a single Next.js codebase which is deployed to different customers, all with different environment variables (such as API URLs, etc). We have a
And in our Dockerfile we simply reference it as an
Essentially, we build our application using the environment variables for one of our customers (typically a test instance with the smallest amounts of data/latency) then, just before the container runs, the A few notes:
|
Beta Was this translation helpful? Give feedback.
-
next experimental-compile seems to be broken in >14.2.0, prior to this, it was working with 14.1.4. NOTE This is running in docker The error I get in 14.2.1 now is:
Prior to the command running, the following environment variables are injected: Some things has changed between the version upgrades, does anyone else have this issue? For context, this is the full build step I used:
Thanks |
Beta Was this translation helpful? Give feedback.
-
I have just tried moving to this for our setup and ultimately reverted the change. I was running the This is a step in the right direction but it is not the ideal solution. What I would like is to make all routes behave like Page Router fallback mode on dynamic routes. Build the code and manifest but do not generate any prerenders, then rely on fallback to lazily build things at runtime. I can see that others have hacked their way to this solution by doing a build and then deleting the *.html and *.rsc output in the next directory. EDIT: I got to the bottom of this. We do not set CPU limits on our pods but we do set memory limits. We end up with too many workers for the available memory. Setting |
Beta Was this translation helpful? Give feedback.
-
Couple questions:
|
Beta Was this translation helpful? Give feedback.
-
I may have encountered a bug related to this? (Maybe not?) - basically, the chunk filenames are not deterministic based on BUILD_ID - is that unexpected behavior? |
Beta Was this translation helpful? Give feedback.
-
Add:
in your root layout.tsx. |
Beta Was this translation helpful? Give feedback.
-
@ijjk Is there any chance that the In my view, this would complete the featureset of the experimental build mode with the |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@ijjk Is there any upcoming change regarding this? Currently, if we run only The original request is still not achievable.
|
Beta Was this translation helpful? Give feedback.
-
Is this feature documented anywhere? It's worth noting, that this is REQUIRED if you want to scale a self hosted set of pods for next.js, when you don't want to (or can't) do the generate step in your build environment. Without it, you hit this bug, which prevents deterministic chunk hashes. But I don't think these experimental build flags are documented anywhere outside of this issue. (In my case, I can't do the generate step in my build environment, because that env doesn't have access to the backend services which supply the data for the that step, and I can't do the compile step in my runtime environment, because it either takes 15 minutes per pod, or produces variable build artifacts, even with a deterministic buildId, or it crashes my entire cluster (especially if I have Sentry enabled). Also, Sentry doesn't seem to support compile/generate at all, which is kind of a bummer.) |
Beta Was this translation helpful? Give feedback.
-
I've found a workaround that you can use if you have middleware.
This will opt the route out of generation during build, but keep the ISR cache. You can't do this for P.S. |
Beta Was this translation helpful? Give feedback.
-
There's another solution. It seems like Next sets a export const revalidate = 1800;
export default async function sitemap() {
if (process.env.NEXT_PHASE === "phase-production-build") return [];
return db.find(/* ... */).map(/* ... */);
} With only this, however, the first render of the page/route would render the empty data generated during the build. So you have to remove the accompanying # ...
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# This right here:
RUN rm -rf /app/.next/server/app/sitemap.xml.*
USER nextjs
# ... My Dockerfile is based on the official one here. With this setup, I get ISR in production mode and with no issues during build. |
Beta Was this translation helpful? Give feedback.
-
Interestingly I get this error and needed to pass in an empty array/object to the |
Beta Was this translation helpful? Give feedback.
-
Describe the feature you'd like to request
In next 13 with app directory there is no way to opt out from build time static page generation without loosing ISR. (Only dynamic render). If we want to use the same code base with different backend server (e.g configured in .env files) we force to use SSR and loose ISR.
These feature allows use case like dynamic portals with different backends servers and different data but the same pages and business logic.
Other issues is that the backend server often cannot be reach by developer/build machine.
In a big portal site this feature will also allow faster build and deploy time too, but preserve the ability to use ISR if needed.
Describe the solution you'd like
Configuration based settings:
Describe alternatives you've considered
Per paged configuration:
or
Beta Was this translation helpful? Give feedback.
All reactions