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

fix for usage of baseURI which doesn't work in IE11 #1562

Merged
merged 5 commits into from
Sep 25, 2020
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
10 changes: 10 additions & 0 deletions runtime/src/app/baseuri_helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function get_base_uri(window_document) {
let baseURI = window_document.baseURI;

if (!baseURI) {
const baseTags = window_document.getElementsByTagName('base');
baseURI = baseTags.length ? baseTags[0].href : window_document.URL;
}

return baseURI;
}
3 changes: 2 additions & 1 deletion runtime/src/app/goto/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { cid, history, navigate, select_target } from '../router';
import { get_base_uri } from '../baseuri_helper';

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

const target = select_target(new URL(href, document.baseURI));
const target = select_target(new URL(href, get_base_uri(document)));

if (target) {
history[opts.replaceState ? 'replaceState' : 'pushState']({ id: cid }, '', href);
Expand Down
3 changes: 2 additions & 1 deletion runtime/src/app/prefetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { hydrate_target } from '../app';
import { select_target } from '../router';
import find_anchor from '../router/find_anchor';
import { HydratedTarget, Target } from '../types';
import { get_base_uri } from '../baseuri_helper';

let prefetching: {
href: string;
Expand All @@ -16,7 +17,7 @@ export function start() {
}

export default function prefetch(href: string) {
const target = select_target(new URL(href, document.baseURI));
const target = select_target(new URL(href, get_base_uri(document)));

if (target) {
if (!prefetching || href !== prefetching.href) {
Expand Down
9 changes: 8 additions & 1 deletion src/core/create_compilers/RollupCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ const inject_styles = `
export default function(files) {
return Promise.all(files.map(function(file) { return new Promise(function(fulfil, reject) {
var href = new URL(file, import.meta.url);
var relative = ('' + href).substring(document.baseURI.length);
var baseURI = document.baseURI;

if (!baseURI) {
var baseTags = document.getElementsByTagName('base');
baseURI = baseTags.length ? baseTags[0].href : document.URL;
}

var relative = ('' + href).substring(baseURI.length);
var link = document.querySelector('link[rel=stylesheet][href="' + relative + '"]')
|| document.querySelector('link[rel=stylesheet][href="' + href + '"]');
if (!link) {
Expand Down
42 changes: 42 additions & 0 deletions test/unit/baseuri_helper/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as assert from 'assert';
import { get_base_uri } from '../../../runtime/src/app/baseuri_helper';

describe('get_base_uri', () => {
it('document.baseURI exists', () => {
const baseUri = 'https://baseuri.example.com';
const document = {
baseURI: baseUri
};
assert.strictEqual(get_base_uri(document), baseUri);
});

it('document.baseURI does not exist, with <base /> tag', () => {
const baseUri = 'https://bytag.example.com';
const document = {
getElementsByTagName: () => [
{ href: baseUri }
]
};
assert.strictEqual(get_base_uri(document), baseUri);
});

it('document.baseURI does not exist, with multiple <base /> tag', () => {
const baseUri = 'https://fromtag.example.com';
const document = {
getElementsByTagName: () => [
{ href: baseUri },
{ href: 'https://ignoreme.example.com' }
]
};
assert.strictEqual(get_base_uri(document), baseUri);
});

it('document.baseURI does not exist, without <base /> tag', () => {
const baseUri = 'https://byurl.example.com';
const document = {
getElementsByTagName: () => [],
URL: baseUri
};
assert.strictEqual(get_base_uri(document), baseUri);
});
});