| 1 |
import { updateHistoryRecord, getCurrentUrl, classify, Location } from '../helpers.js'; |
| 2 |
import type Swup from '../Swup.js'; |
| 3 |
import type { PageData } from './fetchPage.js'; |
| 4 |
import { VisitState, type Visit } from './Visit.js'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Render the next page: replace the content and update scroll position. |
| 8 |
*/ |
| 9 |
export const renderPage = async function (this: Swup, visit: Visit, page: PageData): Promise<void> { |
| 10 |
// Check if failed/aborted in the meantime |
| 11 |
if (visit.done) return; |
| 12 |
|
| 13 |
visit.advance(VisitState.ENTERING); |
| 14 |
|
| 15 |
const { url } = page; |
| 16 |
|
| 17 |
// update state if the url was redirected |
| 18 |
if (!this.isSameResolvedUrl(getCurrentUrl(), url)) { |
| 19 |
updateHistoryRecord(url); |
| 20 |
this.location = Location.fromUrl(url); |
| 21 |
visit.to.url = this.location.url; |
| 22 |
visit.to.hash = this.location.hash; |
| 23 |
} |
| 24 |
|
| 25 |
// replace content: allow handlers and plugins to overwrite paga data and containers |
| 26 |
await this.hooks.call('content:replace', visit, { page }, (visit, { page }) => { |
| 27 |
this.classes.remove('is-leaving'); |
| 28 |
// only add for animated page loads |
| 29 |
if (visit.animation.animate) { |
| 30 |
this.classes.add('is-rendering'); |
| 31 |
} |
| 32 |
const success = this.replaceContent(visit); |
| 33 |
if (!success) { |
| 34 |
throw new Error('[swup] Container mismatch, aborting'); |
| 35 |
} |
| 36 |
if (visit.animation.animate) { |
| 37 |
// Make sure to add these classes to new containers as well |
| 38 |
this.classes.add('is-changing', 'is-animating', 'is-rendering'); |
| 39 |
if (visit.animation.name) { |
| 40 |
this.classes.add(`to-${classify(visit.animation.name)}`); |
| 41 |
} |
| 42 |
} |
| 43 |
}); |
| 44 |
|
| 45 |
// scroll into view: either anchor or top of page |
| 46 |
await this.hooks.call('content:scroll', visit, undefined, () => { |
| 47 |
return this.scrollToContent(visit); |
| 48 |
}); |
| 49 |
|
| 50 |
await this.hooks.call('page:view', visit, { url: this.location.url, title: document.title }); |
| 51 |
}; |