-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use h3 instead of express (#6)
* dfsdfdsfsfs * fsdfsdfsd * fsdfsdfsd * todo: fix redirect * export useH3Event remove "custom" redirect implementation * docs: update lincense year * refactor: replace cheerio with node-html-parser (#5) * update all packages * remove unused counter, rearrange imports * fdsfsfsdfsd * also update peerDependencies * ci(publish): update actions version, use node 20, add packageManager to package.json * add ci workflow run unit-tests * setup pnpm before node * fix(ci): setup pnpm before node in publish workflow * update README * add h3 playground * fix(playgrounds): add missing build targets this fixes the build error because of top level await * refactor: use @unhead/ssr to render SSR head * remove app.use(router) usage * update deps * update node to 22, update every deps * remove console.log from client build * remove build.target array from h3 vite config
- Loading branch information
Showing
31 changed files
with
1,511 additions
and
1,336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: ci | ||
|
||
on: | ||
push: | ||
branches: | ||
- '**' | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
unit-test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: pnpm/action-setup@v4 | ||
|
||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20 | ||
cache: 'pnpm' | ||
|
||
- run: pnpm install | ||
|
||
- name: Run unit tests | ||
run: pnpm run unit-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
20 | ||
22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="vite/client" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<link rel="icon" href="/favicon.ico"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Vite App</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/main.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "h3-example", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "pnpm run build:client && pnpm run build:server", | ||
"build:client": "vite build --ssrManifest --outDir dist/client", | ||
"build:server": "vite build --ssr src/main.ts --outDir dist/server" | ||
}, | ||
"dependencies": { | ||
"@unhead/vue": "^1.11.14", | ||
"h3": "^1.13.0", | ||
"serve-static": "^1.16.2", | ||
"vue": "^3.5.13", | ||
"vue-router": "^4.5.0" | ||
}, | ||
"devDependencies": { | ||
"@tsconfig/node22": "^22.0.0", | ||
"@types/node": "^22.10.2", | ||
"@vitejs/plugin-vue": "^5.2.1", | ||
"@vue/tsconfig": "^0.7.0", | ||
"typescript": "^5.7.2", | ||
"vite": "^6.0.3", | ||
"vite-plugin-vue-ssr": "workspace:*" | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { readFileSync } from 'node:fs' | ||
import { resolve, dirname } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import { createServer } from 'node:http' | ||
import { createApp, defineEventHandler, fromNodeMiddleware, toNodeListener } from 'h3' | ||
import { generateTemplate } from 'vite-plugin-vue-ssr/plugin' | ||
import serveStatic from 'serve-static' | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
|
||
const main = (await import(resolve(__dirname, './dist/server/main.js'))).default | ||
|
||
const template = readFileSync(resolve('dist/client/index.html'), 'utf-8') | ||
|
||
const manifest = JSON.parse( | ||
readFileSync(resolve('dist/client/.vite/ssr-manifest.json'), 'utf-8') | ||
) | ||
|
||
const app = createApp() | ||
|
||
app.use(fromNodeMiddleware(serveStatic(resolve('dist/client'), { index: false }))) | ||
app.use(defineEventHandler(async (event) => { | ||
const url = event.node.req.originalUrl ?? '/' | ||
|
||
const html = await generateTemplate(main, url, template, event, manifest) | ||
|
||
return html | ||
})) | ||
|
||
createServer(toNodeListener(app)).listen(3000, () => console.log('server listening on 3000')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<script setup lang="ts"> | ||
import { useHead } from '@unhead/vue' | ||
useHead({ | ||
title: 'Example app, using vite-plugin-vue-ssr', | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div>H3 examples, see Cookie.vue and Redirect.vue</div> | ||
|
||
<div> | ||
<RouterView /> | ||
</div> | ||
|
||
<div> | ||
Navigating to the components won't do anything... | ||
<ul> | ||
<li><RouterLink to="/">Home</RouterLink></li> | ||
<li><RouterLink to="cookie">Cookie</RouterLink></li> | ||
<li><RouterLink to="redirect">Redirect</RouterLink></li> | ||
</ul> | ||
</div> | ||
</template> | ||
|
||
<style> | ||
body { | ||
background: lightblue; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script setup lang="ts"> | ||
import { useH3Event } from 'vite-plugin-vue-ssr' | ||
import { setCookie } from 'h3' | ||
if (import.meta.env.SSR) { | ||
const event = useH3Event() | ||
setCookie(event, 'cookie-from', 'h3') | ||
} | ||
</script> | ||
|
||
<template> | ||
Me love cookies (check your cookies, they may be gone) | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
Hi from Home | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<script setup lang="ts"> | ||
import { onServerPrefetch } from 'vue' | ||
import { useH3Event } from 'vite-plugin-vue-ssr' | ||
import { getRequestURL, sendRedirect } from 'h3' | ||
// if (import.meta.env.SSR) { | ||
// const event = useH3Event() | ||
// | ||
// const { host } = getRequestURL(event) | ||
// | ||
// sendRedirect(event, `http://${host}/cookie`) | ||
// } | ||
onServerPrefetch(() => { | ||
const event = useH3Event() | ||
const { host } = getRequestURL(event) | ||
sendRedirect(event, `http://${host}/cookie`) | ||
}) | ||
</script> | ||
|
||
<template> | ||
Bye, try refreshing me (it even works in the onServerPrefetch hook) | ||
</template> |
Oops, something went wrong.