Skip to content

Commit

Permalink
fix: use universal-middleware (#16)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: cache related options have been removed

BREAKING CHANGE: the `pageContext` setting has been removed in favor of universal-middleware context

BREAKING CHANGE: `vike-node/connect` replaced by `vike-node/express`

Co-authored-by: Romuald Brillout <[email protected]>
  • Loading branch information
magne4000 and brillout authored Nov 28, 2024
1 parent 25c3d6b commit a7554a7
Show file tree
Hide file tree
Showing 51 changed files with 2,077 additions and 2,587 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"files": {
"ignore": ["dist/", ".vercel/", "package.json"]
"ignore": ["dist/", ".vercel/", "package.json", "test/vike-node/build"]
},
"formatter": {
"indentWidth": 2,
Expand Down
8 changes: 4 additions & 4 deletions examples/express-react-vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"dependencies": {
"@vitejs/plugin-react": "^4.3.1",
"cross-env": "^7.0.3",
"express": "^4.19.2",
"express": "^4.21.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"vike": "^0.4.193",
"vike": "^0.4.198",
"vike-node": "^0.1.16",
"vike-react": "^0.4.18",
"vite": "^5.4.0"
"vike-react": "^0.5.7",
"vite": "^5.4.8"
},
"type": "module"
}
2 changes: 1 addition & 1 deletion examples/express-react-vercel/server/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express'
import vike from 'vike-node/connect'
import vike from 'vike-node/express'

export default startServer()

Expand Down
8 changes: 4 additions & 4 deletions examples/express-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"dependencies": {
"@vitejs/plugin-react": "^4.3.1",
"cross-env": "^7.0.3",
"express": "^4.19.2",
"express": "^4.21.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"vike": "^0.4.193",
"vike": "^0.4.198",
"vike-node": "^0.1.16",
"vike-react": "^0.4.18",
"vite": "^5.4.0"
"vike-react": "^0.5.7",
"vite": "^5.4.8"
},
"type": "module"
}
2 changes: 1 addition & 1 deletion examples/express-react/server/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express'
import vike from 'vike-node/connect'
import vike from 'vike-node/express'

startServer()

Expand Down
6 changes: 3 additions & 3 deletions examples/hono-react-cf-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"hono": "^4.5.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"vike": "^0.4.193",
"vike": "^0.4.198",
"vike-node": "^0.1.16",
"vike-react": "^0.4.18",
"vite": "^5.4.0"
"vike-react": "^0.5.7",
"vite": "^5.4.8"
},
"type": "module",
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions examples/hono-react-vercel-edge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"@hono/node-server": "^1.12.0",
"@vitejs/plugin-react": "^4.3.1",
"cross-env": "^7.0.3",
"hono": "^4.5.5",
"hono": "^4.6.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"vike": "^0.4.193",
"vike": "^0.4.198",
"vike-node": "^0.1.16",
"vike-react": "^0.4.18",
"vite": "^5.4.0",
"vike-react": "^0.5.7",
"vite": "^5.4.8",
"telefunc": "^0.1.76"
},
"type": "module"
Expand Down
2 changes: 1 addition & 1 deletion examples/hono-react-vercel-edge/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
entry: {
index: 'server/node-entry.js',
app: {
path: 'server/app.js',
entry: 'server/app.js',
runtime: 'vercel'
}
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"pnpm": {
"overrides": {
"vike-node": "link:./packages/vike-node/"
"vike-node": "link:./packages/vike-node/",
"esbuild": "^0.24.0"
}
},
"packageManager": "[email protected]"
Expand Down
73 changes: 31 additions & 42 deletions packages/vike-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ In development, the server process is restarted when a change is detected in som
[Installation](#installation)
[Standalone build](#standalone-build)
[External packages](#external-packages)
[Caching and compression](#caching-and-compression)
[Compression](#compression)
[Custom pageContext](#custom-pagecontext)
[Framework examples](#framework-examples)
[Migration guide](#migration-guide)
[Add to existing app](#add-to-existing-app)

<br/>

## Installation

1. `npm install vike-node express`
2. Extend `vite.config.js`:
1. Extend `vite.config.js`:

```js
// vite.config.js
Expand All @@ -36,14 +35,13 @@ In development, the server process is restarted when a change is detected in som
plugins: [vikeNode('server/index.js')]
}
```

3. Create `server/index.js`:
1. Create `server/index.js`:

```js
// server/index.js

import express from 'express'
import vike from 'vike-node/connect'
import vike from 'vike-node/express'

startServer()

Expand All @@ -54,8 +52,9 @@ In development, the server process is restarted when a change is detected in som
app.listen(port, () => console.log(`Server running at http://localhost:${port}`))
}
```
> If you already have a server, see [Add to existing app](#add-to-existing-app).
## Standalone build:
## Standalone build

You can enable standalone builds by setting `standalone` to `true`.
<br>
Expand All @@ -81,7 +80,7 @@ export default {
}
```

## External packages:
## External packages

Packages that import native binaries/custom assets need to be added to `external`.<br>
When building with `standalone` enabled, `external` packages and their assets are copied to the output `dist` directory.<br>
Expand All @@ -108,42 +107,28 @@ export default {
}
```

## Caching and compression:

In production, `vike-node`:
## Compression

- compresses all Vike responses
- caches the compressed static assets(.js, .css).
In production, `vike-node` compresses all Vike responses.

On a request, if the asset(.js, .css) is not in the cache, `vike-node` compresses it with a fast compression level, sends it in the response, then recompresses it with a high compression level and finally caches the compressed data.<br>
You can disable compression/caching:
You can disable it:

```js
app.use(
vike({
compress: false,
static: {
cache: false
}
compress: false
})
)
```

## Custom [pageContext](https://vike.dev/pageContext):
## Custom [pageContext](https://vike.dev/pageContext)

You can define custom [pageContext](https://vike.dev/pageContext) properties:
`vike-node` uses [universal-middleware](https://universal-middleware.dev/) and automatically adds the universal context to `pageContext`.

```js
app.use(
vike({
pageContext: (req) => ({
user: req.user
})
})
)
```
If you need custom properties to be available in `pageContext`,
you can [create a universal context middleware](https://universal-middleware.dev/recipes/context-middleware#updating-the-context) and attach it to your server.

## Framework examples:
## Framework examples

`vike-node` includes middlewares for the most popular web frameworks:

Expand All @@ -153,13 +138,15 @@ app.use(
- H3
- Elysia (Bun)

Express:
[See complete list of supported servers](https://universal-middleware.dev/reference/supported-adapters).

#### Express

```js
// server/index.js

import express from 'express'
import vike from 'vike-node/connect'
import vike from 'vike-node/express'

startServer()

Expand All @@ -171,7 +158,7 @@ function startServer() {
}
```

Fastify:
#### Fastify

```js
// server/index.js
Expand All @@ -183,13 +170,13 @@ startServer()

function startServer() {
const app = fastify()
app.register(vike())
app.all('/*', vike())
const port = 3000
app.listen({ port }, () => console.log(`Server running at http://localhost:${port}`))
}
```

Hono:
#### Hono

```js
// server/index.js
Expand All @@ -214,7 +201,7 @@ function startServer() {
}
```

H3:
#### H3

```js
// server/index.js
Expand All @@ -236,7 +223,7 @@ async function startServer() {
}
```

Elysia (Bun):
#### Elysia (Bun)

```js
// server/index.js
Expand All @@ -248,19 +235,21 @@ startServer()

function startServer() {
const app = new Elysia()
app.use(vike())
app.get('/*', vike())
const port = 3000
app.listen(port, () => console.log(`Server running at http://localhost:${port}`))
}
```

## Migration guide:
## Add to existing app

To add `vike-node` to an existing Vike app:

```diff
// server/index.js

- import { renderPage } from 'vike/server'
+ import { vike } from 'vike-node/connect'
+ import { vike } from 'vike-node/express'

- if (isProduction) {
- app.use(express.static(`${root}/dist/client`))
Expand Down
Loading

0 comments on commit a7554a7

Please sign in to comment.