Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/route-meta-macro
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jan 17, 2022
2 parents 0226991 + 91032cd commit 429c31c
Show file tree
Hide file tree
Showing 21 changed files with 339 additions and 223 deletions.
16 changes: 16 additions & 0 deletions docs/content/3.docs/2.directory-structure/12.server.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ export default async (req: IncomingMessage, res: ServerResponse) => {
}
```

#### Accessing req data

```js
import { useBody, useCookies, useQuery } from 'h3'

export default async (req, res) => {
const query = await useQuery(req)
const body = await useBody(req) // only for POST request
const cookies = useCookies(req)

return { query, body, cookies }
}
```

Learn more about [h3 methods](https://www.jsdocs.io/package/h3#package-index-functions).

## Server Middleware

Nuxt will automatically read in any files in the `~/server/middleware` to create server middleware for your project.
Expand Down
2 changes: 1 addition & 1 deletion examples/use-async-data/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { data, refresh, pending } = await useAsyncData('/api/hello', () => $fetch
</script>

<template>
<NuxtExampleLayout example="use-async-data" show-tips="true">
<NuxtExampleLayout example="use-async-data" show-tips>
<div>{{ data }}</div>
<div>
<NButton :disabled="pending" @click="refresh">
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@
"jiti": "^1.12.9"
},
"devDependencies": {
"@iconify-json/carbon": "^1.0.12",
"@iconify-json/carbon": "^1.0.14",
"@nuxt/ui": "^0.0.0-alpha.5",
"@nuxtjs/eslint-config": "^7.0.0",
"@nuxtjs/eslint-config-typescript": "^7.0.2",
"@types/jsdom": "^16",
"@types/node": "^16.11.19",
"@types/node": "^16.11.20",
"@types/object-hash": "^2",
"@unocss/reset": "^0.22.2",
"@unocss/reset": "^0.22.4",
"esbuild": "^0.14.11",
"eslint": "^8.6.0",
"eslint": "^8.7.0",
"eslint-plugin-jsdoc": "^37.6.1",
"execa": "^6.0.0",
"globby": "^12.0.2",
"globby": "^12.2.0",
"jiti": "^1.12.9",
"lerna": "^4.0.0",
"markdownlint-cli": "^0.30.0",
Expand All @@ -54,7 +54,7 @@
"pathe": "^0.2.0",
"typescript": "^4.5.4",
"unbuild": "^0.6.7",
"vitest": "^0.1.12",
"vitest": "^0.1.18",
"vue-router": "next"
},
"packageManager": "yarn@3.1.1",
Expand Down
10 changes: 5 additions & 5 deletions packages/bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@nuxt/postcss8": "^1.1.3",
"@nuxt/schema": "3.0.0",
"@vitejs/plugin-legacy": "^1.6.4",
"@vue/composition-api": "^1.4.3",
"@vue/composition-api": "^1.4.4",
"@vueuse/head": "^0.7.4",
"acorn": "^8.7.0",
"consola": "^2.15.3",
Expand All @@ -35,13 +35,13 @@
"estree-walker": "^3.0.1",
"externality": "^0.1.5",
"fs-extra": "^10.0.0",
"globby": "^12.0.2",
"globby": "^12.2.0",
"h3": "^0.3.8",
"hash-sum": "^2.0.0",
"magic-string": "^0.25.7",
"mlly": "^0.3.17",
"mlly": "^0.3.18",
"murmurhash-es": "^0.1.1",
"node-fetch": "^3.1.0",
"node-fetch": "^3.1.1",
"nuxi": "3.0.0",
"p-debounce": "^4.0.0",
"pathe": "^0.2.0",
Expand All @@ -55,7 +55,7 @@
"ufo": "^0.7.9",
"unplugin": "^0.3.0",
"unplugin-vue2-script-setup": "0.9.0",
"vite": "2.7.10",
"vite": "^2.7.12",
"vite-plugin-vue2": "^1.9.2",
"vue-template-compiler": "^2.6.14"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/bridge/src/vite/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ export async function buildServer (ctx: ViteBuildContext) {
const viteServer = await vite.createServer(serverConfig)
ctx.nuxt.hook('close', () => viteServer.close())

// Invalidate virtual modules when templates are re-generated
ctx.nuxt.hook('app:templatesGenerated', () => {
for (const [id, mod] of viteServer.moduleGraph.idToModuleMap) {
if (id.startsWith('\x00virtual:')) {
viteServer.moduleGraph.invalidateModule(mod)
}
}
})

// Initialize plugins
await viteServer.pluginContainer.buildStart({})

Expand Down
4 changes: 4 additions & 0 deletions packages/bridge/src/vite/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ async function bundle (nuxt: Nuxt, builder: any) {
mode: nuxt.options.dev ? 'development' : 'production',
logLevel: 'warn',
define: {
'process.static': nuxt.options.target === 'static',
'process.env.NODE_ENV': JSON.stringify(nuxt.options.dev ? 'development' : 'production'),
'process.mode': JSON.stringify(nuxt.options.dev ? 'development' : 'production'),
'process.target': JSON.stringify(nuxt.options.target),
'process.dev': nuxt.options.dev
},
resolve: {
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"@nuxt/schema": "^3.0.0",
"consola": "^2.15.3",
"defu": "^5.0.1",
"dotenv": "^11.0.0",
"globby": "^12.0.2",
"dotenv": "^14.1.0",
"globby": "^12.2.0",
"hash-sum": "^2.0.0",
"jiti": "^1.12.9",
"lodash.template": "^4.5.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/nitro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"esbuild": "^0.14.11",
"etag": "^1.8.1",
"fs-extra": "^10.0.0",
"globby": "^12.0.2",
"globby": "^12.2.0",
"gzip-size": "^7.0.0",
"h3": "^0.3.8",
"hasha": "^5.2.2",
Expand All @@ -51,16 +51,16 @@
"listhen": "^0.2.6",
"mime": "^3.0.0",
"mlly": "^0.3.19",
"node-fetch": "^3.1.0",
"node-fetch": "^3.1.1",
"ohmyfetch": "^0.4.14",
"ora": "^6.0.1",
"p-debounce": "^4.0.0",
"pathe": "^0.2.0",
"pkg-types": "^0.3.2",
"pretty-bytes": "^5.6.0",
"rollup": "^2.63.0",
"rollup": "^2.64.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-visualizer": "^5.5.2",
"rollup-plugin-visualizer": "^5.5.4",
"serve-placeholder": "^1.2.4",
"serve-static": "^1.14.2",
"std-env": "^3.0.1",
Expand All @@ -78,7 +78,7 @@
"@types/node-fetch": "^3.0.2",
"@types/serve-static": "^1.13.10",
"unbuild": "latest",
"vue": "3.2.26"
"vue": "3.2.27"
},
"engines": {
"node": "^14.16.0 || ^16.11.0 || ^17.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/src/rollup/plugins/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import virtual from './virtual'
const unique = (arr: any[]) => Array.from(new Set(arr))

export function middleware (getMiddleware: () => ServerMiddleware[]) {
const getImportId = p => '_' + hasha(p).substr(0, 6)
const getImportId = p => '_' + hasha(p).slice(0, 6)

let lastDump = ''

Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/src/runtime/app/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function renderMiddleware (req, res: ServerResponse) {
let isPayloadReq = false
if (url.startsWith(STATIC_ASSETS_BASE) && url.endsWith(PAYLOAD_JS)) {
isPayloadReq = true
url = url.substr(STATIC_ASSETS_BASE.length, url.length - STATIC_ASSETS_BASE.length - PAYLOAD_JS.length) || '/'
url = url.slice(STATIC_ASSETS_BASE.length, url.length - PAYLOAD_JS.length) || '/'
}

// Initialize ssr context
Expand Down
11 changes: 8 additions & 3 deletions packages/nitro/src/runtime/entries/server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import '#polyfill'
import { Server } from 'http'
import { Server as HttpServer } from 'http'
import { Server as HttpsServer } from 'https'
import destr from 'destr'
import { handle } from '../server'

const server = new Server(handle)
const cert = process.env.NITRO_SSL_CERT
const key = process.env.NITRO_SSL_KEY

const server = cert && key ? new HttpsServer({ key, cert }, handle) : new HttpServer(handle)

const port = (destr(process.env.NUXT_PORT || process.env.PORT) || 3000) as number
const hostname = process.env.NUXT_HOST || process.env.HOST || 'localhost'
Expand All @@ -14,7 +18,8 @@ server.listen(port, hostname, (err) => {
console.error(err)
process.exit(1)
}
console.log(`Listening on http://${hostname}:${port}`)
const protocol = cert && key ? 'https' : 'http'
console.log(`Listening on ${protocol}://${hostname}:${port}`)
})

export default {}
2 changes: 1 addition & 1 deletion packages/nitro/src/server/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function filesToMiddleware (files: string[], baseDir: string, basePath: string,
const route = joinURL(
basePath,
file
.substr(0, file.length - extname(file).length)
.slice(0, file.length - extname(file).length)
.replace(/\/index$/, '')
)
const handle = resolve(baseDir, file)
Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function compileTemplate (contents: string) {
return (params: Record<string, any>) => contents.replace(/{{ ?([\w.]+) ?}}/g, (_, match) => {
const val = dotProp.get(params, match)
if (!val) {
consola.warn(`cannot resolve template param '${match}' in ${contents.substr(0, 20)}`)
consola.warn(`cannot resolve template param '${match}' in ${contents.slice(0, 20)}`)
}
return val as string || `${match}`
})
Expand Down
8 changes: 4 additions & 4 deletions packages/nuxt3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
"@nuxt/schema": "3.0.0",
"@nuxt/vite-builder": "3.0.0",
"@nuxt/webpack-builder": "3.0.0",
"@vue/reactivity": "^3.2.26",
"@vue/shared": "^3.2.26",
"@vue/reactivity": "^3.2.27",
"@vue/shared": "^3.2.27",
"@vueuse/head": "^0.7.4",
"chokidar": "^3.5.2",
"consola": "^2.15.3",
"cookie-es": "^0.5.0",
"defu": "^5.0.1",
"destr": "^1.1.0",
"globby": "^12.0.2",
"globby": "^12.2.0",
"h3": "^0.3.8",
"hash-sum": "^2.0.0",
"hookable": "^5.1.1",
Expand All @@ -46,7 +46,7 @@
"scule": "^0.2.1",
"ufo": "^0.7.9",
"unplugin": "^0.3.0",
"vue": "^3.2.26",
"vue": "^3.2.27",
"vue-router": "^4.0.12"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"devDependencies": {
"@nuxt/schema": "3.0.0",
"unbuild": "latest",
"vue": "3.2.26"
"vue": "3.2.27"
},
"dependencies": {
"@nuxt/kit": "3.0.0",
Expand All @@ -35,12 +35,12 @@
"postcss-import": "^14.0.2",
"postcss-import-resolver": "^2.0.0",
"postcss-url": "^10.1.3",
"rollup-plugin-visualizer": "^5.5.2",
"rollup-plugin-visualizer": "^5.5.4",
"ufo": "^0.7.9",
"vite": "2.7.10"
"vite": "^2.7.12"
},
"peerDependencies": {
"vue": "3.2.26"
"vue": "3.2.27"
},
"engines": {
"node": "^14.16.0 || ^16.11.0 || ^17.0.0"
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/dev-bundler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pathToFileURL } from 'url'
import { existsSync } from 'fs'
import { builtinModules } from 'module'
import { resolve } from 'pathe'
import * as vite from 'vite'
import { ExternalsOptions, isExternal as _isExternal, ExternalsDefaults } from 'externality'
Expand Down Expand Up @@ -73,8 +74,11 @@ async function transformRequest (opts: TransformOptions, id: string) {
// Remove for externals
const withoutVersionQuery = id.replace(/\?v=\w+$/, '')
if (await isExternal(opts, withoutVersionQuery)) {
const path = builtinModules.includes(withoutVersionQuery.split('node:').pop())
? withoutVersionQuery
: pathToFileURL(withoutVersionQuery)
return {
code: `(global, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) => import('${(pathToFileURL(withoutVersionQuery))}').then(r => { exports.default = r.default; ssrExportAll(r) }).catch(e => { console.error(e); throw new Error('[vite dev] Error loading external "${id}".') })`,
code: `(global, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) => import('${path}').then(r => { exports.default = r.default; ssrExportAll(r) }).catch(e => { console.error(e); throw new Error('[vite dev] Error loading external "${id}".') })`,
deps: [],
dynamicDeps: []
}
Expand Down
9 changes: 9 additions & 0 deletions packages/vite/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ export async function buildServer (ctx: ViteBuildContext) {
// Start development server
const viteServer = await vite.createServer(serverConfig)

// Invalidate virtual modules when templates are re-generated
ctx.nuxt.hook('app:templatesGenerated', () => {
for (const [id, mod] of viteServer.moduleGraph.idToModuleMap) {
if (id.startsWith('\x00virtual:')) {
viteServer.moduleGraph.invalidateModule(mod)
}
}
})

// Close server on exit
ctx.nuxt.hook('close', () => viteServer.close())

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export function hash (input: string, length = 8) {
return createHash('sha256')
.update(input)
.digest('hex')
.substr(0, length)
.slice(0, length)
}
6 changes: 3 additions & 3 deletions packages/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"hash-sum": "^2.0.0",
"lodash-es": "^4.17.21",
"memfs": "^3.4.1",
"mini-css-extract-plugin": "^2.4.6",
"mini-css-extract-plugin": "^2.5.0",
"mlly": "^0.3.19",
"pathe": "^0.2.0",
"pify": "^5.0.0",
Expand Down Expand Up @@ -64,10 +64,10 @@
"@types/webpack-hot-middleware": "^2.25.5",
"@types/webpack-virtual-modules": "^0",
"unbuild": "latest",
"vue": "3.2.26"
"vue": "3.2.27"
},
"peerDependencies": {
"vue": "3.2.26"
"vue": "3.2.27"
},
"engines": {
"node": "^14.16.0 || ^16.11.0 || ^17.0.0"
Expand Down
1 change: 0 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
],
"ignoreDeps": [
"docus",
"vite",
"@docus/app",
"@docus/github",
"@docus/social-image",
Expand Down
Loading

0 comments on commit 429c31c

Please sign in to comment.