Skip to content

Routing & Navigation

This guide explains the routing and navigation system using Swup in LamaPress.

Table of Contents


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
  ],
})

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']

The #page element is replaced on navigation.

Caching

Caching is enabled by default:

javascript
cache: true

Pages are cached for faster subsequent navigation.

When a link is clicked:

  1. Swup intercepts click
  2. Checks if link should be handled
  3. Triggers link:click hook
  4. PageTransition.transitionOut() is called
  5. Navigation proceeds

Page Loading

During page load:

  1. Swup fetches new page
  2. Extracts content from #page container
  3. Triggers content:replace hook
  4. Router handles page lifecycle

Content Replacement

On content replace:

  1. Previous page stored via setPreviousPage()
  2. Route change event triggered
  3. New page created via createNextPage()
  4. 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()

Exclude links from Swup:

Add data-no-swup attribute:

html
<a href="/external" data-no-swup>External Link</a>

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>

External links are automatically excluded, but you can be explicit:

html
<a href="https://example.com" target="_blank" rel="noopener">External</a>

3. Clean Up on Navigation

Components should clean up in destroy():

javascript
destroy = () => {
  this.timeline?.kill()
  this.element.removeEventListener('click', this.handleClick)
}

Next Steps:

Released under the MIT License.