Skip to content

Commit

Permalink
Don't check external links on prerender (#1679)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1ack authored Jul 28, 2021
1 parent 4332418 commit 1b18a84
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rare-pots-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

Don't check external links on prerender
8 changes: 8 additions & 0 deletions packages/kit/src/core/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ function get_src(attrs) {
return match && (match[1] || match[2] || match[3]);
}

/** @param {string} attrs */
export function is_rel_external(attrs) {
const match = /rel\s*=\s*(?:["'][^>]*(external)[^>]*["']|(external))/.exec(attrs);
return !!match;
}

/** @param {string} attrs */
function get_srcset_urls(attrs) {
const results = [];
Expand Down Expand Up @@ -225,6 +231,8 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
const attrs = match[2];

if (element === 'a' || element === 'link') {
if (is_rel_external(attrs)) continue;

hrefs.push(get_href(attrs));
} else {
if (element === 'img') {
Expand Down
12 changes: 11 additions & 1 deletion packages/kit/src/core/adapt/prerender.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { get_href } from './prerender.js';
import { get_href, is_rel_external } from './prerender.js';

test('get_href', () => {
assert.equal(get_href('href="/foo" target=""'), '/foo');
assert.equal(get_href('target="" href="/foo"'), '/foo');
});

test('is_rel_external', () => {
assert.equal(is_rel_external('<a href="/foo" rel="external">'), true);
assert.equal(is_rel_external('<a href="/foo" rel = "external">'), true);
assert.equal(is_rel_external('<a href="/foo" rel="license external">'), true);
assert.equal(is_rel_external('<a href="/foo" rel=\'external license\'>'), true);
assert.equal(is_rel_external('<a href="/foo" rel=external>'), true);
assert.equal(is_rel_external('<a href="/foo" rel="stylesheet">'), false);
assert.equal(is_rel_external('<a href="/foo">'), false);
});

test.run();

0 comments on commit 1b18a84

Please sign in to comment.