Skip to content

Commit

Permalink
prerender external links (#3826)
Browse files Browse the repository at this point in the history
* prerender external links

* update tests

* changeset

* lint

* add note about prerendering
  • Loading branch information
Rich-Harris authored Feb 17, 2022
1 parent bf457af commit 58a480e
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 8 deletions.
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"]

0 comments on commit 58a480e

Please sign in to comment.