Skip to content

improve cdnURL handling/behaviour #13098

@danielroe

Description

@danielroe

Background

It should be possible to host generated assets on a CDN, and this should be possible to configure at runtime via Nitro. If omitted, assets should be served from the Nitro static middleware.

Related issues: #12612, #12922

Asset types

There are two kinds of assets we serve from Nitro:

  1. static assets originally from the public/ folder that are not the product of the build
  2. build outputs. As a matter of best practice these should be located in a separate folder so we can apply different caching rules

Proposed configuration

Instead of attempting to configure all of this through router.base and build.publicPath, it makes sense to configure it with the following three values:

  • basePath: /
  • buildAssetsPath: /_nuxt
  • cdnURL?
export default defineNuxtConfig({
  // nuxtApp configuration
  app: {
    basePath, // Relative to ./
    buildAssetsPath, // Relative to basePath
	cdnURL? // Absolute URL 
  }
})

Runtime

At runtime the following values will be calculated, allowing for dynamic override via Nitro (or within server):

  • getRouterBase() - returns the vue router base which should be prepended to all paths for client-side navigation
    computed: basePath
  • getPublicAssetsURL() - returns the public URL that corresponds to .output/public - which might be a CDN URL or the router base
    computed: cdnURL || basePath
  • getBuildAssetsURL() - returns the full computed path to the _nuxt folder that serves build assets
    computed: cdnURL ? ${cdnURL}/${assetsPath} : ${basePath/${assetsPath}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions