Skip to content

Commit

Permalink
feat!: drop paths and support top level baseURL option
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 6, 2022
1 parent b979f74 commit 913f3d8
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 46 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ Auto import options. See [unjs/unimport](https://github.com/unjs/unimport) for m
### Routing
#### `baseURL`
Default: `/`
Server's main base URL.
#### `handlers`
Server handlers and routes.
Expand Down
1 change: 1 addition & 0 deletions playground/nitro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defineNitroConfig({
prerender: {
crawlLinks: true
},
// baseURL: '/app',
routes: {
'/api/swr': { swr: true }
}
Expand Down
15 changes: 6 additions & 9 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve } from 'pathe'
import { loadConfig } from 'c12'
import { klona } from 'klona/full'
import defu from 'defu'
import { withLeadingSlash, withoutTrailingSlash } from 'ufo'
import { withLeadingSlash, withoutTrailingSlash, withTrailingSlash } from 'ufo'
import { resolvePath, detectTarget } from './utils'
import type { NitroConfig, NitroOptions } from './types'
import { runtimeDir, pkgDir } from './dirs'
Expand All @@ -12,13 +12,7 @@ const NitroDefaults: NitroConfig = {
// General
preset: undefined,
logLevel: 3,
runtimeConfig: {
nitro: {
baseURL: '/',
cdnURL: undefined,
buildAssetsDir: 'dist'
}
},
runtimeConfig: { nitro: {} },

// Dirs
scanDirs: [],
Expand All @@ -41,6 +35,7 @@ const NitroDefaults: NitroConfig = {
watchOptions: { ignoreInitial: true },

// Routing
baseURL: '/',
handlers: [],
devHandlers: [],
routes: {},
Expand Down Expand Up @@ -110,8 +105,10 @@ export async function loadOptions (userConfig: NitroConfig = {}): Promise<NitroO
options.scanDirs = [options.srcDir]
}

options.baseURL = withLeadingSlash(withTrailingSlash(options.baseURL))
options.runtimeConfig = defu(options.runtimeConfig, {
app: {
nitro: {
baseURL: options.baseURL,
routes: options.routes
}
})
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const app = createApp({
onError: handleError
})

app.use(timingMiddleware)
app.use(config.nitro.baseURL, timingMiddleware)

const router = createRouter()

Expand All @@ -32,13 +32,13 @@ for (const h of handlers) {
}

if (h.route === '/') {
app.use(handler)
app.use(config.nitro.baseURL, handler)
} else {
router.use(h.route, handler)
}
}

app.use(router)
app.use(config.nitro.baseURL, router)

export const localCall = createCall(app.nodeHandler as any)
export const localFetch = createLocalFetch(localCall, globalThis.fetch)
Expand Down
19 changes: 10 additions & 9 deletions src/runtime/entries/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'
import { withoutBase } from 'ufo'
import { localCall } from '../app'
import { requestHasBody, useRequestBody } from '../utils'
import { buildAssetsURL, baseURL } from '#nitro/paths'
import { config } from '#nitro'

addEventListener('fetch', (event: any) => {
event.respondWith(handleEvent(event))
Expand Down Expand Up @@ -41,17 +41,18 @@ async function handleEvent (event) {
})
}

function assetsCacheControl (request) {
if (request.url.startsWith(buildAssetsURL())) {
return {
browserTTL: 31536000,
edgeTTL: 31536000
}
}
function assetsCacheControl (_request) {
// TODO: Detect public asset bases
// if (request.url.startsWith(buildAssetsURL())) {
// return {
// browserTTL: 31536000,
// edgeTTL: 31536000
// }
// }
return {}
}

const baseURLModifier = (request: Request) => {
const url = withoutBase(request.url, baseURL())
const url = withoutBase(request.url, config.nitro.baseURL)
return mapRequestToAsset(new Request(url, request))
}
4 changes: 2 additions & 2 deletions src/runtime/entries/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Server as HttpServer } from 'http'
import { Server as HttpsServer } from 'https'
import destr from 'destr'
import { app } from '../app'
import { baseURL } from '#nitro/paths'
import { config } from '#nitro'

const cert = process.env.NITRO_SSL_CERT
const key = process.env.NITRO_SSL_KEY
Expand All @@ -20,7 +20,7 @@ server.listen(port, hostname, (err) => {
process.exit(1)
}
const protocol = cert && key ? 'https' : 'http'
console.log(`Listening on ${protocol}://${hostname}:${port}${baseURL()}`)
console.log(`Listening on ${protocol}://${hostname}:${port}${config.nitro.baseURL}`)
})

export default {}
3 changes: 1 addition & 2 deletions src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { storage } from '#nitro/virtual/storage'
export { config } from './config'
export * from './cache'
export * from './paths'
export { defineCachedFunction, defineCachedEventHandler } from './cache'
19 changes: 0 additions & 19 deletions src/runtime/paths.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ export interface NitroOptions {
/** @deprecated Use top-level routes option! */
routes: NitroRoutesOptions
baseURL: string,
cdnURL: string,
buildAssetsDir: string
}
[key: string]: any
}
Expand Down Expand Up @@ -110,6 +108,7 @@ export interface NitroOptions {
watchOptions: WatchOptions

// Routing
baseURL: string,
handlers: NitroEventHandler[]
routes: NitroRoutesOptions
devHandlers: NitroDevEventHandler[]
Expand Down

0 comments on commit 913f3d8

Please sign in to comment.