-
Notifications
You must be signed in to change notification settings - Fork 6
/
_main.mjs
175 lines (154 loc) 路 5.2 KB
/
_main.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import trimSlashes from '../utils/client/trim-slashes'
import wipeAnimation from '../utils/client/wipe-animation'
import intersection from '../utils/client/set-intersection'
import { getPageDiff, dataPageAttrs } from '../utils/client/get-page-diff'
import scrollIntoView from '../utils/client/scroll-into-view'
const noop = () => {}
let prevPathname = location.pathname
let cleanupFns = []
let onLoadingFns = []
let onLoadedFns = []
let loading = false
const loadNewStyles = (styles) =>
Promise.all(
styles.map(
(style) =>
new Promise((resolve, reject) => {
const newStyle = document.head.appendChild(style)
newStyle.onload = resolve
// TODO: stop silently failing on bad stylesheets
// Necessary evil right now, since we always have styles even when the file is nonexistent!
newStyle.onerror = resolve
})
)
)
const resolveStyles = async (fullPage) => {
const stylesheetSelector = 'head > link[rel="stylesheet"]'
const oldStyles = [...document.querySelectorAll(stylesheetSelector)]
const newStyles = [...fullPage.querySelectorAll(stylesheetSelector)]
const overlappingStyles = intersection(
oldStyles.map(({ href }) => href),
newStyles.map(({ href }) => href)
)
await loadNewStyles(
// only load styles that *aren't already on the page*
newStyles.filter((style) => !overlappingStyles.has(style.href))
)
return () =>
oldStyles.forEach((style) => {
if (!overlappingStyles.has(style.href)) {
document.head.removeChild(style)
}
})
}
const animatePageIntoView = async (fullPage) => {
const [page, prevPage] = getPageDiff(fullPage, document)
const layoutContainer = prevPage.parentElement
layoutContainer.style.position = 'relative'
layoutContainer.insertBefore(page, prevPage)
await new Promise((resolve) => {
requestAnimationFrame(async () => {
await wipeAnimation(page, prevPage, () =>
layoutContainer.removeChild(prevPage)
)
resolve()
})
})
}
const yoinkHTML = async (href) => {
const response = await fetch(href)
const htmlString = await response.text()
const page = new DOMParser().parseFromString(htmlString, 'text/html')
const title = page.querySelector('title').innerText
return { page, title }
}
const yoinkLayoutJS = async (pageHTML = document) => {
const layoutNames = dataPageAttrs(pageHTML)
.map((attr) => attr.value)
.filter((dataPage) => dataPage.startsWith('_layouts'))
layoutNames.push('_layouts')
return await Promise.all(layoutNames.map((layoutName) => yoinkJS(layoutName)))
}
const yoinkJS = async (pathname) => {
try {
const pathnameWithTrailingSlash = pathname ? pathname + '/' : pathname
const jsModule = await import(`./${pathnameWithTrailingSlash}__client.mjs`)
const onLoading = (callback = () => {}) => {
onLoadingFns.push(callback)
}
if (!jsModule) return noop
const data = jsModule.use11tyData
? await import(`./${pathnameWithTrailingSlash}__data.mjs`)
: {}
return () => jsModule.default({ data: data.default, onLoading })
} catch (e) {
return noop
}
}
const setPageTitle = (title = '') => {
const titleEl = document.querySelector('title')
if (titleEl) {
titleEl.innerText = title
} else {
const titleEl = document.createElement('title')
titleEl.innerText = title
document.documentElement.appendChild(titleEl)
}
}
const popAndRun = (cleanupArr = []) => {
while (cleanupArr.length) cleanupArr.pop()()
}
const runAndPushReturn = (jsArr = [], cleanupArr = []) => {
for (const fn of jsArr) cleanupArr.push(fn() ?? noop)
}
const sequenceProcessHTMLLayoutJS = async (href = '') => {
const { page, title } = await yoinkHTML(href)
const layoutJSList = await yoinkLayoutJS(page)
return { page, title, layoutJSList }
}
const setVisiblePage = async ({ pathname = '', href = '' }) => {
loading = true
setTimeout(() => {
if (loading === true) runAndPushReturn(onLoadingFns, onLoadedFns)
}, 100)
const [{ page, title, layoutJSList }, mainJS] = await Promise.all([
sequenceProcessHTMLLayoutJS(href),
yoinkJS(trimSlashes(pathname)),
])
setPageTitle(title)
const removeOldStyles = await resolveStyles(page)
loading = false
popAndRun(onLoadedFns)
popAndRun(cleanupFns)
await animatePageIntoView(page)
removeOldStyles()
// TODO: on quick navigation, it's possible to run the next page function
// before the previous cleanup finishes
// will probably need a queuing system to solve this
runAndPushReturn([mainJS, ...layoutJSList], cleanupFns)
prevPathname = pathname
}
document.addEventListener('click', async (event) => {
const { target } = event
if (target.tagName === 'A' && target.origin === location.origin) {
event.preventDefault()
scrollIntoView(event)
if (target.pathname !== prevPathname) {
history.pushState({}, null, target.href)
await setVisiblePage(target)
}
}
})
onpopstate = () => {
if (location.hash === '' && location.pathname !== prevPathname) {
setVisiblePage(location)
}
}
// on startup
;(async () => {
const [mainJS, layoutJSList] = await Promise.all([
yoinkJS(trimSlashes(location.pathname)),
yoinkLayoutJS(),
])
runAndPushReturn([mainJS, ...layoutJSList], cleanupFns)
})()