Skip to content

Commit

Permalink
[fix] discover new routes in dev server (#2198)
Browse files Browse the repository at this point in the history
Pass a function to get an up-to-date manifest data
  • Loading branch information
jasonlyu123 authored Aug 15, 2021
1 parent e5aa4f8 commit b3b4382
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-jeans-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix new route discovery in dev server
16 changes: 12 additions & 4 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,15 @@ class Watcher extends EventEmitter {

this.vite = await vite.createServer(merged_config);

handler = await create_handler(this.vite, this.config, this.dir, this.cwd, this.manifest);
const get_manifest = () => {
if (!this.manifest) {
throw new Error('Manifest is not available');
}

return this.manifest;
};

handler = await create_handler(this.vite, this.config, this.dir, this.cwd, get_manifest);

this.server.listen(this.port, this.host || '0.0.0.0');
}
Expand Down Expand Up @@ -267,9 +275,9 @@ function get_params(array) {
* @param {import('types/config').ValidatedConfig} config
* @param {string} dir
* @param {string} cwd
* @param {import('types/internal').SSRManifest} manifest
* @param {() => import('types/internal').SSRManifest} get_manifest
*/
async function create_handler(vite, config, dir, cwd, manifest) {
async function create_handler(vite, config, dir, cwd, get_manifest) {
/**
* @type {amp_validator.Validator?}
*/
Expand Down Expand Up @@ -429,7 +437,7 @@ async function create_handler(vite, config, dir, cwd, manifest) {
styles: Array.from(styles)
};
},
manifest,
manifest: get_manifest(),
prerender: config.kit.prerender.enabled,
read: (file) => fs.readFileSync(path.join(config.kit.files.assets, file)),
root,
Expand Down
28 changes: 27 additions & 1 deletion packages/kit/test/apps/basics/src/routes/routing/_tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as assert from 'uvu/assert';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

/** @type {import('test').TestMaker} */
export default function (test) {
export default function (test, is_dev) {
test(
'redirects from /routing/ to /routing',
'/routing/slashes',
Expand Down Expand Up @@ -238,4 +241,27 @@ export default function (test) {
assert.equal(await page.url(), 'https://www.google.com/');
}
);

test('watch new route in dev', '/routing', async ({ page, base, js, watcher }) => {
if (!is_dev || js) {
return;
}

// hash the filename so that it won't conflict with
// future test file that has the same name
const route = 'bar' + new Date().valueOf();
const content = 'Hello new route';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, `${route}.svelte`);

try {
fs.writeFileSync(filePath, `<h1>${content}</h1>`);
watcher.update();
await page.goto(`${base}/routing/${route}`);

assert.equal(await page.textContent('h1'), content);
} finally {
fs.unlinkSync(filePath);
}
});
}

0 comments on commit b3b4382

Please sign in to comment.