diff --git a/docs/guide/build.md b/docs/guide/build.md index ab5d64707f3684..cb4a8eef6cd583 100644 --- a/docs/guide/build.md +++ b/docs/guide/build.md @@ -259,24 +259,26 @@ experimental: { If the hashed assets and public files aren't deployed together, options for each group can be defined independently using asset `type` included in the second `context` param given to the function. + ```ts twoslash import type { UserConfig } from 'vite' import path from 'node:path' const config: UserConfig = { - // ---cut-before--- - experimental: { - renderBuiltUrl(filename, { hostId, hostType, type }) { - if (type === 'public') { - return 'https://www.domain.com/' + filename - } else if (path.extname(hostId) === '.js') { - return { runtime: `window.__assetsPath(${JSON.stringify(filename)})` } - } else { - return 'https://cdn.domain.com/assets/' + filename - } - }, +// ---cut-before--- +experimental: { + renderBuiltUrl(filename, { hostId, hostType, type }) { + if (type === 'public') { + return 'https://www.domain.com/' + filename + } else if (path.extname(hostId) === '.js') { + return { runtime: `window.__assetsPath(${JSON.stringify(filename)})` } + } else { + return 'https://cdn.domain.com/assets/' + filename + } }, - // ---cut-after--- +}, +// ---cut-after--- } ``` + Note that the `filename` passed is a decoded URL, and if the function returns a URL string, it should also be decoded. Vite will handle the encoding automatically when rendering the URLs. If an object with `runtime` is returned, encoding should be handled yourself where needed as the runtime code will be rendered as is.