Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit ab9ab40

Browse files
authored
add baseURI fallback for IE11 (#1562)
1 parent 5bf164d commit ab9ab40

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

runtime/src/app/baseuri_helper.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function get_base_uri(window_document) {
2+
let baseURI = window_document.baseURI;
3+
4+
if (!baseURI) {
5+
const baseTags = window_document.getElementsByTagName('base');
6+
baseURI = baseTags.length ? baseTags[0].href : window_document.URL;
7+
}
8+
9+
return baseURI;
10+
}

runtime/src/app/goto/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { cid, history, navigate, select_target } from '../router';
2+
import { get_base_uri } from '../baseuri_helper';
23

34
export default function goto(
45
href: string,
56
opts: { noscroll?: boolean, replaceState?: boolean } = { noscroll: false, replaceState: false }): Promise<void> {
67

7-
const target = select_target(new URL(href, document.baseURI));
8+
const target = select_target(new URL(href, get_base_uri(document)));
89

910
if (target) {
1011
history[opts.replaceState ? 'replaceState' : 'pushState']({ id: cid }, '', href);

runtime/src/app/prefetch/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { hydrate_target } from '../app';
22
import { select_target } from '../router';
33
import find_anchor from '../router/find_anchor';
44
import { HydratedTarget, Target } from '../types';
5+
import { get_base_uri } from '../baseuri_helper';
56

67
let prefetching: {
78
href: string;
@@ -16,7 +17,7 @@ export function start() {
1617
}
1718

1819
export default function prefetch(href: string) {
19-
const target = select_target(new URL(href, document.baseURI));
20+
const target = select_target(new URL(href, get_base_uri(document)));
2021

2122
if (target) {
2223
if (!prefetching || href !== prefetching.href) {

src/core/create_compilers/RollupCompiler.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ const inject_styles = `
2525
export default function(files) {
2626
return Promise.all(files.map(function(file) { return new Promise(function(fulfil, reject) {
2727
var href = new URL(file, import.meta.url);
28-
var relative = ('' + href).substring(document.baseURI.length);
28+
var baseURI = document.baseURI;
29+
30+
if (!baseURI) {
31+
var baseTags = document.getElementsByTagName('base');
32+
baseURI = baseTags.length ? baseTags[0].href : document.URL;
33+
}
34+
35+
var relative = ('' + href).substring(baseURI.length);
2936
var link = document.querySelector('link[rel=stylesheet][href="' + relative + '"]')
3037
|| document.querySelector('link[rel=stylesheet][href="' + href + '"]');
3138
if (!link) {

test/unit/baseuri_helper/test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as assert from 'assert';
2+
import { get_base_uri } from '../../../runtime/src/app/baseuri_helper';
3+
4+
describe('get_base_uri', () => {
5+
it('document.baseURI exists', () => {
6+
const baseUri = 'https://baseuri.example.com';
7+
const document = {
8+
baseURI: baseUri
9+
};
10+
assert.strictEqual(get_base_uri(document), baseUri);
11+
});
12+
13+
it('document.baseURI does not exist, with <base /> tag', () => {
14+
const baseUri = 'https://bytag.example.com';
15+
const document = {
16+
getElementsByTagName: () => [
17+
{ href: baseUri }
18+
]
19+
};
20+
assert.strictEqual(get_base_uri(document), baseUri);
21+
});
22+
23+
it('document.baseURI does not exist, with multiple <base /> tag', () => {
24+
const baseUri = 'https://fromtag.example.com';
25+
const document = {
26+
getElementsByTagName: () => [
27+
{ href: baseUri },
28+
{ href: 'https://ignoreme.example.com' }
29+
]
30+
};
31+
assert.strictEqual(get_base_uri(document), baseUri);
32+
});
33+
34+
it('document.baseURI does not exist, without <base /> tag', () => {
35+
const baseUri = 'https://byurl.example.com';
36+
const document = {
37+
getElementsByTagName: () => [],
38+
URL: baseUri
39+
};
40+
assert.strictEqual(get_base_uri(document), baseUri);
41+
});
42+
});

0 commit comments

Comments
 (0)