Searching Puzzle

Type to search Puzzle.

Wire the Router

Walkthrough part 2: add a layout, a second page, params, and navigation between them.

Part 1 left the spellbook as a single view with its data trapped in the file. This part gives the app real structure: shared data both pages can read, a layout that wraps every page, and a detail route with a URL parameter.

Share the data

Two views are about to need the same spells, so the array moves out of the view into a module. Create app/lib/spells.js:

// app/lib/spells.js
export const SPELLS = [
  { id: 's1', name: 'Featherfall',  school: 'Transmutation', level: 1 },
  { id: 's2', name: 'Mage Armor',   school: 'Abjuration',    level: 1 },
  { id: 's3', name: 'Misty Step',   school: 'Conjuration',   level: 2 },
  { id: 's4', name: 'Counterspell', school: 'Abjuration',    level: 3 },
  { id: 's5', name: 'Polymorph',    school: 'Transmutation', level: 4 },
];

In Spellbook.pzl, delete the local SPELLS const and import it instead. This is deliberately still a plain array; noticing its limits is the setup for part 3.

Add a layout

A layout is chrome that persists across pages. The routed view renders at the <Slot/> marker. Replace the scaffold's app/layouts/Default.pzl with:

<puzzle-view class="shell">
  <header>
    <a href="{ link('/') }">Spellbook</a>
  </header>
  <main>
    <Slot/>
  </main>
</puzzle-view>

<script>
import { PuzzleView } from '@magic-spells/puzzle';

export default class DefaultLayout extends PuzzleView {}
</script>

Navigation is a plain <a href>. The link() function makes the href portable across router modes: it emits a normal path in path mode and a hash href in hash mode, from the same template.

The detail view

Create app/views/SpellDetail.pzl. It reads which spell to show from the URL via params.id:

<puzzle-view class="spell-detail">
  {#if spell}
    <h1>{ spell.name }</h1>
    <p>{ spell.school } · level { spell.level }</p>
  {:else}
    <h1>Unknown spell</h1>
  {/if}
  <a href="{ link('/') }">← All spells</a>
</puzzle-view>

<script>
import { PuzzleView } from '@magic-spells/puzzle';
import { SPELLS } from '@/lib/spells.js';

export default class SpellDetail extends PuzzleView {
  data(params, props) {
    return {
      spell: SPELLS.find((s) => s.id === params.id) ?? null,
    };
  }
}
</script>

Route params always arrive as strings: params.id here is 's3', not a number. That's fine for our string ids, and when the store takes over in part 3 it won't matter at all, because record lookup treats 7 and '7' as the same key.

The route table

Replace app/routes.js with three routes: list, detail, and a catch-all:

import Spellbook from '@/views/Spellbook.pzl';
import SpellDetail from '@/views/SpellDetail.pzl';
import NotFound from '@/views/NotFound.pzl';
import DefaultLayout from '@/layouts/Default.pzl';

export default [
  {
    path: '/',
    name: 'spellbook',
    view: Spellbook,
    layout: DefaultLayout,
    meta: { title: 'Spellbook' },
  },
  {
    path: '/spells/:id',
    name: 'spell-detail',
    view: SpellDetail,
    layout: DefaultLayout,
    meta: { title: 'Spell · Spellbook' },
  },
  {
    path: '*',
    name: 'not-found',
    view: NotFound,
    layout: DefaultLayout,
    meta: { title: 'Not found · Spellbook' },
  },
];

Worth naming as you read it:

  • The route table is pure data. Views here are statically imported; for a view you'd rather download on demand, lazy() wraps the import (see Routing).
  • :id captures a param. Routes match in order, and the * catch-all always matches last.
  • meta.title sets the document title per route, inherited leaf to root and synced on every navigation.

The catch-all needs a view to render. The scaffold already created app/views/NotFound.pzl, with a heading and a link home; it's the page every unmatched URL lands on.

One caveat to file away

If you later prerender your app to static HTML, a :param route like /spells/:id is prerendered once per staticPaths entry; without one it is skipped with a warning. It doesn't affect this walkthrough's SPA, but it's the first thing to revisit in Static Builds when you get there.

Next: Add Models, the payoff: schema-backed data and views that react on their own.