Config cache using routeRules without effecting inner paths
I'm trying to cache responses on the client with max-age 30 and stale-while-revalidate 30.
The endpoint is /api/featureA/<id>.
The UI page that makes the request for this endpoint also makes additional api calls to /api/featureA/<id>/something?param=value.
How can I config routeRules that catches only /api/featureA/<id> but not handling the inner one?
I tried settings something like this but then nuxt returns 404 for the inner request
11 Replies
Hi @lazygopher. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To apply a route rule that matches only the direct path
You may need to explicitly list the routes you want to cache if the set of IDs is known at build time, or handle the caching logic inside your API handler for more granular control. Summary:
The knowledge sources do not provide a way to match only
- Hybrid Rendering: Route Rules - extendRouteRules API ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
/api/featureA/<id>
(e.g., /api/featureA/123
) but not nested paths like /api/featureA/123/something
, you should use a single asterisk (*
) in your route pattern. However, according to the knowledge sources, the pattern "/api/featureA/*"
will match both /api/featureA/123
and /api/featureA/123/something
, because the single asterisk matches any path segment at that level and below.
Nuxt's routeRules do not natively support a pattern that matches only a single path segment after a base path (i.e., only /api/featureA/:id
and not deeper paths) using wildcards. The double asterisk (**
) matches all nested routes, and a single asterisk (*
) matches one segment, but there is no built-in way to exclude deeper paths using only the routeRules pattern syntax.
If you set:
This will match both /api/featureA/123
and /api/featureA/123/something
, which is not what you want. There is no documented way in the knowledge sources to restrict the rule to only one segment after /api/featureA/
and exclude deeper paths.
Workaround:You may need to explicitly list the routes you want to cache if the set of IDs is known at build time, or handle the caching logic inside your API handler for more granular control. Summary:
The knowledge sources do not provide a way to match only
/api/featureA/:id
and not /api/featureA/:id/something
using routeRules wildcards. You may need to use explicit route listing or custom logic in your handler for this use case.
References:- Hybrid Rendering: Route Rules - extendRouteRules API ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@693201794137981049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai what's the difference between routeRules { prerender: true } and { static: true } ?
Let me check the knowledge base for you again. One moment! :books:
----------------------
Based on the provided knowledge sources, only the
-
prerender: true
property is documented for use in routeRules
. Setting { prerender: true }
in routeRules
tells Nuxt to prerender the specified route at build time, generating a static HTML file for that route. This improves performance and SEO by serving prebuilt pages instead of generating them on the fly Hybrid Rendering: Route Rules.
There is no mention of a static: true
property for routeRules
in the official Nuxt documentation or related sources. Therefore, I cannot provide a comparison or explanation for static: true
as it is not a documented or supported option in the knowledge sources.
Summary:-
{ prerender: true }
in routeRules
enables prerendering of the route at build time.
- { static: true }
is not documented or supported in the knowledge sources, so its effect (if any) is unknown.
If you need static generation, use { prerender: true }
in your routeRules
configuration.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@693201794137981049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai when using prerender: true, but build stuck and not finishing (more than an hour). What can be the issue?
Let me check the knowledge base for you again. One moment! :books:
----------------------
When using
If you set
The Nitro crawler works by following links from your root page and any statically linked pages. If your site has a very large number of pages or circular links, the crawler can get stuck or take a very long time to finish Prerendering Docs. 3. API or External Calls During Prerender:
If your pages make API calls during prerendering and those APIs are slow, unavailable, or require authentication (e.g., returning 401 errors), the build can hang or fail. This is especially common if environment variables or credentials are missing in your build environment Prerender on DigitalOcean gives 401. 4. Build Environment Issues:
Sometimes, the build may hang due to environment-specific issues, such as missing dependencies, platform differences (e.g., building in Docker with a different architecture), or resource constraints. For example, switching from
prerender: true
in your routeRules
and the build is stuck or not finishing (taking more than an hour), possible causes based on the knowledge sources include:
1. Prerendering Unreachable or Dynamic Routes:If you set
prerender: true
on a wildcard or dynamic route (e.g., /api/featureA/*
or /blog/**
), Nuxt may not know which specific paths to prerender, especially for dynamic routes. This can cause the build to hang as it tries to resolve or crawl routes that are not explicitly defined or discoverable. For dynamic routes, you need to provide explicit paths using the nitro.prerender.routes
option or ensure your pages are linked from other pages so the crawler can find them Prerendering Docs fix(nuxt): use routeRules
to hint pages to prerender.
2. Crawl-based Prerendering Issues:The Nitro crawler works by following links from your root page and any statically linked pages. If your site has a very large number of pages or circular links, the crawler can get stuck or take a very long time to finish Prerendering Docs. 3. API or External Calls During Prerender:
If your pages make API calls during prerendering and those APIs are slow, unavailable, or require authentication (e.g., returning 401 errors), the build can hang or fail. This is especially common if environment variables or credentials are missing in your build environment Prerender on DigitalOcean gives 401. 4. Build Environment Issues:
Sometimes, the build may hang due to environment-specific issues, such as missing dependencies, platform differences (e.g., building in Docker with a different architecture), or resource constraints. For example, switching from
docker build
to docker buildx build
resolved a hanging build in one case Nuxt build hanging when building in linux docker image.
What you can do:
- Check your routeRules
and make sure you are not prerendering dynamic routes without specifying the actual paths.
- Use the nitro.prerender.routes
option to explicitly list dynamic routes to prerender.
- Ensure all APIs called during prerendering are accessible and do not require missing credentials.
- Review your build environment for any platform or dependency issues.
If you need more specific help, try running the build with more verbose logging or check for errors related to API calls or missing environment variables.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@693201794137981049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai does it matter if I run nuxt build with --prerender param? If I have prerender routes but running just nuxt buid?
Let me check the knowledge base for you again. One moment! :books:
----------------------
Yes, it does matter if you run
If you want your prerender routes to be generated as static HTML, you must use
nuxt build
with the --prerender
parameter.
- Running nuxt build --prerender
will build your Nuxt application and also prerender static routes according to your configuration (such as routeRules
with { prerender: true }
or nitro.prerender.routes
). This is similar to what nuxt generate
does, but nuxt build --prerender
is the explicit way to trigger prerendering during the build process nuxt build command.
- If you run just nuxt build
(without --prerender
), it will build your application for server or hybrid deployment, but it will not prerender static routes—even if you have prerender: true
in your routeRules
. The prerendering step is only triggered when you use --prerender
or run nuxt generate
nuxt build command prerendering docs.
Summary:If you want your prerender routes to be generated as static HTML, you must use
nuxt build --prerender
or nuxt generate
. Running just nuxt build
will not prerender those routes.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@693201794137981049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.