Skip to content

Commit c58be6f

Browse files
committed
Full-reload SWUP navigations that cross ExDoc builds
We force a full reload if there is a `/` anywhere in the path. Fix issue introduced in a66397f where anchors are not swupped.
1 parent a806765 commit c58be6f

3 files changed

Lines changed: 48 additions & 11 deletions

File tree

assets/js/swup.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const maybeMetaRedirect = (visit, {page}) => {
2121

2222
window.addEventListener('DOMContentLoaded', emitExdocLoaded)
2323

24+
export const LINK_SELECTOR = 'a[href]:not([href^="http"]):not([href*="/"]):is([href$=".html"], [href*=".html#"], [href^="#"])'
25+
2426
if (!isEmbedded && window.location.protocol !== 'file:') {
2527
new Swup({
2628
animationSelector: false,
@@ -30,7 +32,7 @@ if (!isEmbedded && window.location.protocol !== 'file:') {
3032
return path === window.location.pathname ||
3133
path === window.location.pathname + '.html'
3234
},
33-
linkSelector: 'a[href]:not([href^="/"]):not([href^="http"])[href$=".html"]',
35+
linkSelector: LINK_SELECTOR,
3436
hooks: {
3537
'page:load': maybeMetaRedirect,
3638
'page:view': emitExdocLoaded

assets/test/swup.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { el } from '../js/helpers'
2+
import { isCrossBuild, LINK_SELECTOR } from '../js/swup'
3+
4+
const linkMatches = (href) => el('a', {href}).matches(LINK_SELECTOR)
5+
6+
describe('swup', () => {
7+
it('should absolute links', () => {
8+
expect(linkMatches('/foo.html')).toBe(false)
9+
})
10+
11+
it('should ignore external links', () => {
12+
expect(linkMatches('http://example.com/foo.html')).toBe(false)
13+
expect(linkMatches('https://example.com/foo.html')).toBe(false)
14+
})
15+
16+
it('should ignore links with slashes', () => {
17+
expect(linkMatches('foo/bar.html')).toBe(false)
18+
})
19+
20+
it('should ignore any page not ending with .html', () => {
21+
expect(linkMatches('foo')).toBe(false)
22+
expect(linkMatches('foo.js')).toBe(false)
23+
})
24+
25+
it('should match relative links to other pages', () => {
26+
expect(linkMatches('bar.html')).toBe(true)
27+
})
28+
29+
it('should match local links with anchors', () => {
30+
expect(linkMatches('foo.html#section')).toBe(true)
31+
expect(linkMatches('foo#section')).toBe(false)
32+
expect(linkMatches('#section')).toBe(true)
33+
expect(linkMatches('bar/foo.html#section')).toBe(false)
34+
})
35+
})

0 commit comments

Comments
 (0)