Appearance
Routing & Navigation
This guide explains the routing and navigation system using Swup in LamaPress.
Table of Contents
- Overview
- Swup Configuration
- Navigation Flow
- Router Methods
- Excluding Links
- Best Practices
- Related Documentation
Overview
LamaPress uses Swup for client-side routing, providing smooth page transitions without full page reloads. The Router class manages navigation, page lifecycle, and Swup integration.
Swup Configuration
Swup is configured in src/js/core/router/index.js:
javascript
this.swup = new Swup({
containers: ['#page'],
cache: true,
linkSelector: 'a[href^="/"]',
animateHistoryBrowsing: false,
plugins: [
// ... plugins
],
})1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Link Selection
Links are selected automatically:
Included:
- Links to same origin (
a[href^="${window.location.origin}"]) - Relative links (
a[href^="/"]) - Hash links (
a[href^="#"])
Excluded:
- Download links (
[download]) - External links (
[target="_blank"]) - Links with
data-no-swup - PDF links (
[href$=".pdf"])
Containers
Content is replaced in:
javascript
containers: ['#page']1
The #page element is replaced on navigation.
Caching
Caching is enabled by default:
javascript
cache: true1
Pages are cached for faster subsequent navigation.
Navigation Flow
Link Click
When a link is clicked:
- Swup intercepts click
- Checks if link should be handled
- Triggers
link:clickhook - PageTransition.transitionOut() is called
- Navigation proceeds
Page Loading
During page load:
- Swup fetches new page
- Extracts content from
#pagecontainer - Triggers
content:replacehook - Router handles page lifecycle
Content Replacement
On content replace:
- Previous page stored via
setPreviousPage() - Route change event triggered
- New page created via
createNextPage() - Page enters via
enterNextPage()
Router Methods
Page Lifecycle
Router manages page lifecycle:
leavePreviousPage()
- Leaves previous page
- Returns Promise
destroyPreviousPage()
- Destroys previous page and components
createNextPage(nextPage)
- Creates new Page instance
- Initializes components
enterNextPage()
- Enters new page
- Triggers component
enter()methods
getPages()
- Returns
{ nextPage, previousPage }elements
Helper Functions
Use helper to get router methods:
javascript
import { getRouterMethods } from '@src/js/core/router'
const { leavePreviousPage, createNextPage, enterNextPage } = getRouterMethods()1
2
3
2
3
Excluding Links
Exclude links from Swup:
Add data-no-swup attribute:
html
<a href="/external" data-no-swup>External Link</a>1
Or use excluded selectors:
[download][target="_blank"][href$=".pdf"]
Best Practices
1. Use data-no-swup
Exclude links that shouldn't use Swup:
html
<a href="/external" data-no-swup>External Link</a>
<a href="/file.pdf" data-no-swup>Download PDF</a>1
2
2
2. Handle External Links
External links are automatically excluded, but you can be explicit:
html
<a href="https://example.com" target="_blank" rel="noopener">External</a>1
3. Clean Up on Navigation
Components should clean up in destroy():
javascript
destroy = () => {
this.timeline?.kill()
this.element.removeEventListener('click', this.handleClick)
}1
2
3
4
2
3
4
Related Documentation
- Page Transitions - Transition system
- JavaScript Architecture - JavaScript system
- Component JavaScript - Component lifecycle
Next Steps:
- Review Page Transitions for transition details
- Check JavaScript Architecture for system overview
- See Component JavaScript for component lifecycle