emit a basepath event on first run

This commit is contained in:
Rich Harris
2018-06-14 17:20:46 -04:00
parent 9c4a3592ff
commit 9eeeaa24c1
4 changed files with 35 additions and 3 deletions

View File

@@ -58,6 +58,8 @@ export default function middleware({ App, routes, store }: {
const client_assets = JSON.parse(fs.readFileSync(path.join(output, 'client_assets.json'), 'utf-8'));
let emitted_basepath = false;
const middleware = compose_handlers([
(req: Req, res: ServerResponse, next: () => void) => {
if (req.baseUrl === undefined) {
@@ -66,6 +68,16 @@ export default function middleware({ App, routes, store }: {
: '';
}
if (!emitted_basepath && process.send) {
process.send({
__sapper__: true,
event: 'basepath',
basepath: req.baseUrl
});
emitted_basepath = true;
}
if (req.path === undefined) {
req.path = req.url.replace(/\?.*/, '');
}
@@ -276,6 +288,7 @@ function get_route_handler(chunks: Record<string, string>, App: Component, route
if (process.send) {
process.send({
__sapper__: true,
event: 'file',
url: req.url,
method: req.method,
status: 200,
@@ -315,6 +328,7 @@ function get_route_handler(chunks: Record<string, string>, App: Component, route
process.send({
__sapper__: true,
event: 'file',
url: req.url,
method: req.method,
status: res.statusCode,