Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Dec 1, 2023
1 parent 0bff9e6 commit 7926d78
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ function kit({ svelte_config }) {
scoped(
assets,
sirv(join(svelte_config.kit.outDir, 'output/client'), {
setHeaders: (res, pathname) => {
setHeaders: (res, pathname) => {
// only apply to immutable directory, not e.g. version.json
if (pathname.startsWith(`/${svelte_config.kit.appDir}/immutable`)) {
res.setHeader('cache-control', 'public,max-age=31536000,immutable');
Expand Down
98 changes: 48 additions & 50 deletions packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,69 +60,67 @@ export async function preview(vite, vite_config, svelte_config) {

// prerendered pages (we can't just use sirv because we need to
// preserve the correct trailingSlash behaviour)
vite.middlewares.use(
(req, res, next) => {
let if_none_match_value = req.headers['if-none-match'];
vite.middlewares.use((req, res, next) => {
let if_none_match_value = req.headers['if-none-match'];

if (if_none_match_value?.startsWith('W/"')) {
if_none_match_value = if_none_match_value.substring(2);
}
if (if_none_match_value?.startsWith('W/"')) {
if_none_match_value = if_none_match_value.substring(2);
}

if (if_none_match_value === etag) {
res.statusCode = 304;
res.end();
return;
}
if (if_none_match_value === etag) {
res.statusCode = 304;
res.end();
return;
}

const { pathname, search } = new URL(/** @type {string} */ (req.url), 'http://dummy');

let filename = normalizePath(
join(svelte_config.kit.outDir, 'output/prerendered/pages' + pathname)
);
let prerendered = is_file(filename);

if (!prerendered) {
const has_trailing_slash = pathname.endsWith('/');
const html_filename = `${filename}${has_trailing_slash ? 'index.html' : '.html'}`;

/** @type {string | undefined} */
let redirect;

if (is_file(html_filename)) {
filename = html_filename;
prerendered = true;
} else if (has_trailing_slash) {
if (is_file(filename.slice(0, -1) + '.html')) {
redirect = pathname.slice(0, -1);
}
} else if (is_file(filename + '/index.html')) {
redirect = pathname + '/';
}
const { pathname, search } = new URL(/** @type {string} */ (req.url), 'http://dummy');

let filename = normalizePath(
join(svelte_config.kit.outDir, 'output/prerendered/pages' + pathname)
);
let prerendered = is_file(filename);

if (redirect) {
if (search) redirect += search;
res.writeHead(307, {
location: redirect
});
if (!prerendered) {
const has_trailing_slash = pathname.endsWith('/');
const html_filename = `${filename}${has_trailing_slash ? 'index.html' : '.html'}`;

res.end();
/** @type {string | undefined} */
let redirect;

return;
if (is_file(html_filename)) {
filename = html_filename;
prerendered = true;
} else if (has_trailing_slash) {
if (is_file(filename.slice(0, -1) + '.html')) {
redirect = pathname.slice(0, -1);
}
} else if (is_file(filename + '/index.html')) {
redirect = pathname + '/';
}

if (prerendered) {
res.writeHead(200, {
'content-type': lookup(pathname) || 'text/html',
etag
if (redirect) {
if (search) redirect += search;
res.writeHead(307, {
location: redirect
});

fs.createReadStream(filename).pipe(res);
} else {
next();
res.end();

return;
}
}
);

if (prerendered) {
res.writeHead(200, {
'content-type': lookup(pathname) || 'text/html',
etag
});

fs.createReadStream(filename).pipe(res);
} else {
next();
}
});

// SSR
vite.middlewares.use(async (req, res) => {
Expand Down

0 comments on commit 7926d78

Please sign in to comment.