Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prerender external links #3826

Merged
merged 6 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-berries-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] Crawl rel="external" links when prerendering
2 changes: 2 additions & 0 deletions documentation/docs/07-a-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ Adding a `rel=external` attribute to a link...
```

...will trigger a browser navigation when the link is clicked.

> SvelteKit does not exclude root-relative external links from prerendering, which will cause 404s if these URLs are intended to be served by a separate app. Use a custom [`prerender.onError`](/docs/configuration#prerender) handler if you need to ignore them.
9 changes: 2 additions & 7 deletions packages/kit/src/core/adapt/prerender/crawl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const TAG_OPEN = /[a-zA-Z]/;
const TAG_CHAR = /[a-zA-Z0-9]/;
const ATTRIBUTE_NAME = /[^\t\n\f />"'=]/;

const EXTERNAL = /\bexternal\b/;

const WHITESPACE = /[\s\n\r]/;

/** @param {string} html */
Expand Down Expand Up @@ -88,7 +86,6 @@ export function crawl(html) {
}
}

let rel = '';
let href = '';

while (i < html.length) {
Expand Down Expand Up @@ -149,9 +146,7 @@ export function crawl(html) {
i -= 1;
}

if (name === 'rel') {
rel = value;
} else if (name === 'href') {
if (name === 'href') {
href = value;
} else if (name === 'src') {
hrefs.push(value);
Expand Down Expand Up @@ -183,7 +178,7 @@ export function crawl(html) {
i += 1;
}

if (href && !EXTERNAL.test(rel)) {
if (href) {
hrefs.push(href);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["/styles.css", "/favicon.png", "https://external.com", "/wheee"]