UNPKG

vike

Version:

The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.

34 lines (33 loc) 1.39 kB
export { urlToFile }; export { baseServer }; import { assert } from './assert.js'; import { parseUrl } from './parseUrl.js'; import { slice } from './slice.js'; // - When doing a `.pageContext.json` HTTP request, the base URL should be preserved. (The server-side will handle the base URL.) // - While pre-rendering there is no base URL const baseServer = '/'; function urlToFile(url, fileExtension, doNotCreateExtraDirectory) { const { pathnameOriginal, searchOriginal, hashOriginal } = parseUrl(url, baseServer); if (url.startsWith('/')) { assert(url === `${pathnameOriginal}${searchOriginal || ''}${hashOriginal || ''}`, { url }); } const hasTrailingSlash = pathnameOriginal.endsWith('/'); let pathnameModified; if (doNotCreateExtraDirectory && pathnameOriginal !== '/') { if (hasTrailingSlash) { pathnameModified = slice(pathnameOriginal, 0, -1); } else { pathnameModified = pathnameOriginal; } assert(!pathnameModified.endsWith('/'), { url }); assert(pathnameModified !== ''); } else { pathnameModified = pathnameOriginal + (hasTrailingSlash ? '' : '/') + 'index'; } assert(pathnameModified); pathnameModified = pathnameModified + fileExtension; const fileUrl = `${pathnameModified}${searchOriginal || ''}${hashOriginal || ''}`; return fileUrl; }