-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: simplify
playground/ssr-react
(#397)
- Loading branch information
Showing
8 changed files
with
90 additions
and
670 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,10 +1,78 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import { defineConfig } from 'vite' | ||
import react from '@vitejs/plugin-react' | ||
|
||
process.env.MY_CUSTOM_SECRET = 'API_KEY_qwertyuiop' | ||
|
||
export default defineConfig({ | ||
server: { port: 8907 /* Should be unique */ }, | ||
plugins: [react()], | ||
appType: 'custom', | ||
build: { | ||
minify: false, | ||
}, | ||
environments: { | ||
client: { | ||
build: { | ||
outDir: 'dist/client', | ||
}, | ||
}, | ||
ssr: { | ||
build: { | ||
outDir: 'dist/server', | ||
rollupOptions: { | ||
input: 'src/entry-server.jsx', | ||
}, | ||
}, | ||
}, | ||
}, | ||
plugins: [ | ||
react(), | ||
{ | ||
name: 'ssr-middleware', | ||
configureServer(server) { | ||
return () => { | ||
server.middlewares.use(async (req, res, next) => { | ||
const url = req.originalUrl ?? '/' | ||
try { | ||
const { render } = await server.ssrLoadModule( | ||
'/src/entry-server.jsx', | ||
) | ||
const appHtml = render(url) | ||
const template = await server.transformIndexHtml( | ||
url, | ||
fs.readFileSync(path.resolve('index.html'), 'utf-8'), | ||
) | ||
const html = template.replace(`<!--app-html-->`, appHtml) | ||
res.setHeader('content-type', 'text/html').end(html) | ||
} catch (e) { | ||
next(e) | ||
} | ||
}) | ||
} | ||
}, | ||
async configurePreviewServer(server) { | ||
const template = fs.readFileSync( | ||
path.resolve('dist/client/index.html'), | ||
'utf-8', | ||
) | ||
const { render } = await import( | ||
new URL('./dist/server/entry-server.js', import.meta.url).href | ||
) | ||
return () => { | ||
server.middlewares.use(async (req, res, next) => { | ||
const url = req.originalUrl ?? '/' | ||
try { | ||
const appHtml = render(url) | ||
const html = template.replace(`<!--app-html-->`, appHtml) | ||
res.setHeader('content-type', 'text/html').end(html) | ||
} catch (e) { | ||
next(e) | ||
} | ||
}) | ||
} | ||
}, | ||
}, | ||
], | ||
// tell vitestSetup.ts to use buildApp API | ||
builder: {}, | ||
}) |
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
Oops, something went wrong.