Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #453 from turbolinks/async-script-fix
Browse files Browse the repository at this point in the history
Fix <script async> navigation
  • Loading branch information
sstephenson committed Mar 8, 2019
2 parents 0e88eec + f2c40b7 commit 26f42b1
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/controller.ts
Expand Up @@ -95,7 +95,7 @@ export class Controller {
// History

startHistory() {
this.location = Location.wrap(window.location.toString())
this.location = Location.currentLocation
this.restorationIdentifier = uuid()
this.history.start()
this.history.replace(this.location, this.restorationIdentifier)
Expand Down Expand Up @@ -145,8 +145,8 @@ export class Controller {
if (this.shouldCacheSnapshot()) {
this.notifyApplicationBeforeCachingSnapshot()
const snapshot = this.view.getSnapshot()
const location = this.lastRenderedLocation
defer(() => this.cache.put(location!, snapshot.clone()))
const location = this.lastRenderedLocation || Location.currentLocation
defer(() => this.cache.put(location, snapshot.clone()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/history.ts
Expand Up @@ -46,7 +46,7 @@ export class History {
if (this.shouldHandlePopState()) {
const { turbolinks } = event.state
if (turbolinks) {
const location = Location.wrap(window.location.toString())
const location = Location.currentLocation
const { restorationIdentifier } = turbolinks
this.delegate.historyPoppedToLocationWithRestorationIdentifier(location, restorationIdentifier)
}
Expand Down
4 changes: 4 additions & 0 deletions src/location.ts
@@ -1,6 +1,10 @@
export type Locatable = Location | string

export class Location {
static get currentLocation() {
return this.wrap(window.location.toString())
}

static wrap(locatable: Locatable): Location
static wrap(locatable?: Locatable | null): Location | undefined
static wrap(locatable: Locatable) {
Expand Down
20 changes: 20 additions & 0 deletions src/tests/async_script_tests.ts
@@ -0,0 +1,20 @@
import { TurbolinksTestCase } from "./helpers/turbolinks_test_case"

export class AsyncScriptTests extends TurbolinksTestCase {
async setup() {
await this.goToLocation("/fixtures/async_script.html")
}

async "test does not emit turbolinks:load when loaded asynchronously after DOMContentLoaded"() {
const events = await this.eventLogChannel.read()
this.assert.deepEqual(events, [])
}

async "test following a link when loaded asynchronously after DOMContentLoaded"() {
this.clickSelector("#async-link")
await this.nextBody
this.assert.equal(await this.visitAction, "advance")
}
}

AsyncScriptTests.registerSuite()
24 changes: 24 additions & 0 deletions src/tests/fixtures/async_script.html
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Turbolinks</title>
<script src="/fixtures/test.js"></script>
<script>
addEventListener("DOMContentLoaded", function() {
setTimeout(function() {
var script = document.createElement("script")
script.src = "/fixtures/turbolinks.js"
script.setAttribute("async", "")
document.head.appendChild(script)
}, 1)
})
</script>
</head>
<body>
<section>
<h1>Async script</h1>
<p><a href="async_script_2.html" id="async-link">Async script 2</a></p>
</section>
</body>
</html>
14 changes: 14 additions & 0 deletions src/tests/fixtures/async_script_2.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Turbolinks</title>
<script src="/fixtures/test.js"></script>
<script src="/fixtures/turbolinks.js" async></script>
</head>
<body>
<section>
<h1>Async script 2</h1>
</section>
</body>
</html>
1 change: 1 addition & 0 deletions src/tests/index.ts
@@ -1,3 +1,4 @@
export * from "./async_script_tests"
export * from "./navigation_tests"
export * from "./rendering_tests"
export * from "./visit_tests"

0 comments on commit 26f42b1

Please sign in to comment.