Skip to content

publicAssets with maxAge not working in some cases #3587

Closed

Description

@hi-ogawa

Environment

stackblitz with "nitro": "npm:nitro-nightly@latest"

Reproduction

https://stackblitz.com/edit/github-83dbpmnp?file=nitro.config.ts

Describe the bug

With the following config nitro config with publicAssets and maxAge:

export default defineNitroConfig({
  srcDir: 'server',
  publicAssets: [
    // maxAge at root doesn't work for any presets?
    {
      dir: './client/',
      baseURL: '/',
      maxAge: 10,
    },
    // maxAge anywhere doesn't work for node-server preset?
    {
      dir: './client/nested/',
      baseURL: '/nested/',
      maxAge: 20,
    },
  ],
});

and building with node-server preset,

NITRO_PRESET=node-server nitro build
node .output/server/index.mjs

neither of asset response includes cache-control.

curl -v http://127.0.0.1:3000/asset1.txt
curl -v http://127.0.0.1:3000/nested/asset2.txt

Building with vercel preset NITRO_PRESET=vercel nitro build, it generates a following .vercel/output/config.json, which I'm not sure where max-age=31536000 is coming from and whether this is working intended.

{
  "version": 3,
  "overrides": {},
  "routes": [
    {
      "headers": {
        "cache-control": "public, max-age=20, immutable"
      },
      "src": "/nested/(.*)"
    },
    {
      "src": "/nested(.*)",
      "headers": {
        "cache-control": "public,max-age=31536000,immutable"
      },
      "continue": true
    },
    {
      "handle": "filesystem"
    },
    {
      "src": "/(.*)",
      "dest": "/__fallback"
    }
  ]
}

Additional context

One question: I noticed Nitro has routeRules which allows custom headers. I haven't tried, but is routeRoles preferred over nesting publicAssets like above?

Personally root assets not having cache-control seems fine, but being able to add cache-control only for some directory inside should work (at least either via publicAssets or routeRules).

I think this is the same issue I'm seeing in #3586 with the default node-server preset.

Logs

Activity

  1. added
    pending triage
    on Sep 24, 2025
  2. added
    bug
    and removed
    pending triage
    on Sep 24, 2025
  3. pi0 commented on Sep 24, 2025

    @pi0
    Member

    Thanks for the issue. In fact Nitro extends route rules (as single source of trust) from public assets (src).

    Note: Base URL of / is considered a "fall though" (because it can overlap with other routes therefore we don't have long term caching for those assets unless clearly prefixed with something like /client or /_nuxt, etc)

    There is a chance we regressed this behavior in recent v3 changes. I will investigate more.

  4. pi0 commented on Sep 24, 2025

    @pi0
    Member

    Found regression, after compiled routing (#3575) route rules were being applied after global middleware (serve static was one of them) therefore missing additional headers. 9fe8cd4 fixes is.

  5. hi-ogawa commented on Sep 25, 2025

    @hi-ogawa
    ContributorAuthor

    Thanks for the issue. In fact Nitro extends route rules (as single source of trust) from public assets (src).

    Note: Base URL of / is considered a "fall though" (because it can overlap with other routes therefore we don't have long term caching for those assets unless clearly prefixed with something like /client or /_nuxt, etc)

    Thanks for the pointer. That's good to know 🙏 Yes, the root fall through makes sense and my repro above with maxAge was for artificial purpose. I just wanted to confirm the behavior.


    For vercel routes, it generated two more routes with max-age=20 and max-age=31536000. Is this expected?

  6. pi0x commented on Sep 7, 2026

    @pi0x
    Contributor

    Answering the leftover question about the second Vercel route: no, that was a bug. The Vercel preset emitted its own cache-control route for every non-fallthrough publicAssets entry with max-age=31536000 hardcoded, ignoring the maxAge you set, so you got two routes for the same directory.

    Fixed in be23b43 — the header is now built from the directory's maxAge (one year only when unset) and is skipped entirely when a route rule already set cache-control. Building your config on current main now emits just the max-age=20 route for /nested/(.*), and node-server sends cache-control: public, max-age=20, immutable on /nested/asset2.txt. Root assets still get nothing, which is the fall-through behaviour pi0 described.

    Posted by an AI triage agent on behalf of a maintainer.

  7. added theissue type on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions