Skip to content

Commit

Permalink
feat!: update viewer dev middleware to be compatible with h3@0.8 (#545
Browse files Browse the repository at this point in the history
)

Co-authored-by: Daniel Roe <daniel@roe.dev>
  • Loading branch information
DamianGlowala and danielroe authored Oct 19, 2022
1 parent 1766484 commit 0b1844e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"clear-module": "^4.1.2",
"consola": "^2.15.3",
"defu": "^6.1.0",
"h3": "^0.8.1",
"postcss": "^8.4.16",
"postcss-custom-properties": "^12.1.8",
"postcss-nesting": "^10.1.10",
Expand Down
12 changes: 6 additions & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
isNuxt3, findPath, requireModule
} from '@nuxt/kit'
import { Config } from 'tailwindcss'
import { eventHandler, sendRedirect } from 'h3'
import { name, version } from '../package.json'
import vitePlugin from './hmr'
import defaultTailwindConfig from './tailwind.config'
Expand Down Expand Up @@ -231,13 +232,12 @@ export default defineNuxtModule<ModuleOptions>({
const { withTrailingSlash, withoutTrailingSlash } = await import('ufo')
const routerPrefix = isNuxt3() ? route : undefined
const _viewerDevMiddleware = createServer({ tailwindConfigProvider: () => tailwindConfig, routerPrefix }).asMiddleware()
const viewerDevMiddleware = (req, res) => {
if (req.originalUrl === withoutTrailingSlash(route)) {
res.writeHead(301, { Location: withTrailingSlash(req.originalUrl) })
return res.end()
const viewerDevMiddleware = eventHandler((event) => {
if (event.req.url === withoutTrailingSlash(route)) {
return sendRedirect(event, withTrailingSlash(event.req.url), 301)
}
_viewerDevMiddleware(req, res)
}
_viewerDevMiddleware(event.req, event.res)
})
if (isNuxt3()) { addDevServerHandler({ route, handler: viewerDevMiddleware }) }
if (isNuxt2()) { nuxt.options.serverMiddleware.push({ route, handler: viewerDevMiddleware }) }
nuxt.hook('listen', (_, listener) => {
Expand Down
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4203,6 +4203,16 @@ h3@^0.7.12, h3@^0.7.21:
radix3 "^0.1.2"
ufo "^0.8.5"

h3@^0.8.1:
version "0.8.1"
resolved "https://registry.yarnpkg.com/h3/-/h3-0.8.1.tgz#e9992f09dd1abcf2c190d4cd068febf9bddafd41"
integrity sha512-HWTShxx4RKpse3f2h5KOWTEAIZLKq9SHWaVBZkOhBH+fH8uRGYY1iNO7VDwImFwARtR/Pg+bVI8feXX9NIdQRQ==
dependencies:
cookie-es "^0.5.0"
destr "^1.1.1"
radix3 "^0.2.1"
ufo "^0.8.5"

handlebars@^4.7.7:
version "4.7.7"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.7.tgz#9ce33416aad02dbd6c8fafa8240d5d98004945a1"
Expand Down Expand Up @@ -7155,6 +7165,11 @@ radix3@^0.1.2:
resolved "https://registry.yarnpkg.com/radix3/-/radix3-0.1.2.tgz#5f7351af7fc5e4b1d9a1b14a7266b6a4a8cac0ba"
integrity sha512-Mpfd/OuX0zoJ6ojLD/RTOHvJPg6e6PjINtmYzV87kIXc5iUtDz34i7gg4SV4XjqRJTmSiYO/g9i/mKWGf4z8wg==

radix3@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/radix3/-/radix3-0.2.1.tgz#77e66a41c7ba5600a8bc137fd259ef661d314418"
integrity sha512-FnhArTl5Tq7dodiLeSPKrDUyCQuJqEncP8cKdyy399g8F/cz7GH6FmzA3Rkosu2IZMkpswFFwXfb2ERSiL06pg==

randombytes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
Expand Down

0 comments on commit 0b1844e

Please sign in to comment.