mirror of https://github.com/sveltejs/svelte
chore: fix playground prod preview (#12334)
* chore: fix playground prod preview * tweak * tweakpull/12339/head
parent
5eff68f63d
commit
d1432b0ab3
@ -1,12 +0,0 @@
|
||||
import { mount, hydrate, unmount } from 'svelte';
|
||||
import App from './src/main.svelte';
|
||||
|
||||
const root = document.getElementById('root')!;
|
||||
const render = root.firstChild?.nextSibling ? hydrate : mount;
|
||||
|
||||
const component = render(App, {
|
||||
target: document.getElementById('root')!
|
||||
});
|
||||
|
||||
// @ts-ignore
|
||||
window.unmount = () => unmount(component);
|
@ -1,22 +0,0 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import express from 'express';
|
||||
import { head, body } from './server/entry-server.js';
|
||||
|
||||
const rendered = fs
|
||||
.readFileSync(path.resolve('./dist/client/index.html'), 'utf-8')
|
||||
.replace(`<!--ssr-html-->`, body)
|
||||
.replace(`<!--ssr-head-->`, head);
|
||||
|
||||
express()
|
||||
.use('*', async (req, res) => {
|
||||
if (req.originalUrl !== '/') {
|
||||
res.sendFile(path.resolve('./dist/client' + req.originalUrl));
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(200).set({ 'Content-Type': 'text/html' }).end(rendered);
|
||||
})
|
||||
.listen('3000');
|
||||
|
||||
console.log('listening on http://localhost:3000');
|
@ -0,0 +1,41 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import polka from 'polka';
|
||||
import { render } from 'svelte/server';
|
||||
import App from './src/main.svelte';
|
||||
|
||||
const { head, body } = render(App);
|
||||
|
||||
const rendered = fs
|
||||
.readFileSync(path.resolve('./dist/client/index.html'), 'utf-8')
|
||||
.replace(`<!--ssr-html-->`, body)
|
||||
.replace(`<!--ssr-head-->`, head);
|
||||
|
||||
const types = {
|
||||
'.js': 'application/javascript',
|
||||
'.css': 'text/css'
|
||||
};
|
||||
|
||||
polka()
|
||||
.use((req, res) => {
|
||||
if (req.url === '/') {
|
||||
res.writeHead(200, { 'content-type': 'text/html' });
|
||||
res.end(rendered);
|
||||
return;
|
||||
}
|
||||
|
||||
const file = path.resolve('./dist/client' + req.url);
|
||||
|
||||
if (fs.existsSync(file)) {
|
||||
const type = types[path.extname(req.url)] ?? 'application/octet-stream';
|
||||
res.writeHead(200, { 'content-type': type });
|
||||
fs.createReadStream(file).pipe(res);
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(404);
|
||||
res.end('not found');
|
||||
})
|
||||
.listen('3000');
|
||||
|
||||
console.log('listening on http://localhost:3000');
|
Loading…
Reference in new issue