Defining routes

Like every full-stack meta-framework, routes are the central concept of Gracile.
This is where everything got tied together, and besides that, and add-ons, there aren’t many opinions left.

Gracile comes with a dedicated function that will take care of typings as with JS or TS, and nothing more.

Under the hood, it uses the URLPattern API which uses converted patterns like this:

Index

You have two ways to define an index for a folder.

  1. Using like: foo/my-folder/index.ts
  2. With parentheses like: foo/(anything).ts, foo/(foo).ts

Respectively:

  1. /src/routes/foo/index.ts/foo/
  2. /src/routes/foo/(foo).ts/foo/

The parentheses pattern is especially useful for quick file switching with your IDE, where a lot of indexes can be confusing, same when debugging an error trace.
Note that when indexes are noted that way, the first one will be chosen (alphabetically).

Ignored files and directories

defineRoute parameters

The defineRoute provides a type-safe API that can be used with JavaScript or TypeScript.
It’s analog to how numerous OSS projects are providing their configuration API (like Vite’s defineConfig).

document

Provides the base document for the route’s page template.
Given a pre-existing document, you’ll import it like this in your route configuration:

📄 /src/document.ts

import { html } from '@gracile/gracile/server-html';

export const document = (options: { url: URL }) => html`
  <html>
    <head>
      <!-- ... -->
      <title>${url.pathname}</title>
    </head>
    <body>
      <route-template-outlet></route-template-outlet>
    </body>
  </html>
`;

📄 /src/routes/my-page.ts

import { html } from 'lit';
import { defineRoute } from '@gracile/gracile/route';
import { document } from '../document.js'; 

export default defineRoute({
  document: (context) => document(context),

  template: () => html`...`,
});

template

Provides a server-renderable template.
When combined with an enclosing document, we’ll call it a “Page”.
When used alone, we’ll call it an HTML “Fragment”.

📄 /src/routes/my-page.ts

import { html } from 'lit';
import { defineRoute } from '@gracile/gracile/route';

export default defineRoute({
  // ...
  template: (context) => html`
    <main>
      <article class="prose">Hello</article>
    </main>
  `,
});

staticPaths

Used with static mode only.

You can provide props and params for populating page data.

template and document contexts will be properly typed thanks to the staticPaths function return signature.
Hover context.props and context.params to see!

📄 /src/routes/[...path].ts

import { html } from 'lit';
import { defineRoute } from '@gracile/gracile/route';

import { document } from '../document.js';

export default defineRoute({
  staticPaths: () =>
    [
      {
        params: { path: 'my/first-cat' },
        props: { cat: 'Kino' },
      },
      {
        params: { path: 'my/second-cat' },
        props: { cat: 'Valentine' },
      },
    ] as const,

  document: (context) => document({ ...context, title: context.props.cat }),

   Hover the tokens to see the typings reflection.
  template: async (context) => html`
    <h1>${context.props.cat}</h1>
    <main>${context.url.pathname}</main>
    <footer>${context.params.path}</footer>
  `,
});

handler (experimental)

Used with server mode only.
Like staticPaths, handler is a provider for props and can receive the current — matched route — params.

There are two behaviors for the handlers:

  1. Returning an instance of Response will terminate the pipeline, without going through the template rendering that happens afterward otherwise.
    Useful for redirects, pure JSON API routes…

  2. Returning anything else will provide the typed props for the template to consume.

Minimal example:


📄 /src/routes/index.ts

import { html } from 'lit';
import { defineRoute } from '@gracile/gracile/route';
import { document } from '../document.js';

const achievements = [{ name: 'initial' }];

export default defineRoute({
  handler: {
    POST: async (context) => {
      const formData = await context.request.formData();

      const name = formData.get('achievement')?.toString();

      if (name) achievements.push({ name });

      return Response.redirect(context.url, 303);
    },
  },

  document: (context) => document(context),

  template: async (context) => html`
    <form method="post">
      <input type="text" name="achievement" />
      <button>Add an "Achievement"</button>
    </form>

    <ul>
      ${achievements.map((achievement) => html`<li>${achievement.name}</li>`)}
    </ul>
  `,
});

See also the “Forms” recipe for a full, contextualized example.

HTTP methods

Note that, per the HTML specs, only GET and POST can be used with an HTML <form> element.
Other methods like DELETE, PUT, etc. can be used, but Gracile won’t pursue the route template rendering with them.
A new method, “QUERY”, is also inside the radar, and will possibly be implemented in node:http and other server environments.

Minimal example

📄 /src/routes/my-page.ts

import { html } from 'lit';
import { defineRoute } from '@gracile/gracile/route';
import { document } from '../document.js';

export default defineRoute({
  document: (context) => document({ ...context, title: 'My Page' }),

  template: ({ url }) => html`
    <main class="content">
      <article class="prose">
        <!-- ... -->
        Hello ${url.pathname}
        <!-- ... -->
      </article>
    </main>
  `,
});

Bare pages (for redirects, etc.)

Sometimes, you don’t want to bring a page template in a route, just a bare HTML document, maybe with some <meta>; perfect use-case: page redirects.

It’s totally possible to skip the template altogether and just use a single document.

Here, we will redirect the user to another URL, while collecting some analytics, all that with a nice and simple transitive screen:

📄 /src/routes/chat.ts

import { defineRoute } from '@gracile/gracile/route';
import { html } from '@gracile/gracile/server-html';

import discordLogo from '../assets/icons/discord.svg' with { type: 'svg-lit' };
import { DISCORD_INVITE_URL } from '../content/global.js';
import { googleAnalytics } from '../document-helpers.js';

const waitTime = 2;

export default defineRoute({
  document: () => html`
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        ${googleAnalytics}

        <style>
          & {
            margin: calc(10dvh + 10dvw);
            font-family: system-ui;
            font-size: 2rem;
            text-align: center;
            color-scheme: dark light;
          }

          svg {
            height: 3rem;
            width: 3rem;
          }
        </style>

        <title>Gracile - Discord Server (redirecting…)</title>

         The current page, "https://gracile.js.org/chat/", will be forgotten from history after the redirection.  
        <meta
          http-equiv="refresh"
          content=${`${waitTime};URL=${DISCORD_INVITE_URL}`}
        />
      </head>

      <body>
        ${discordLogo}

        <p>Redirecting to the Discord invitation link…</p>
         No need for the <route-template-outlet> here! 
      </body>
    </html>
  `,
});

What to put in routes?

Data fetching, dispatching, per-route assets to bundle, components, templates, modules, features…

It’s generally better to use routes as entry points and not put too much UI or logic in there, besides what’s strictly needed to bootstrap the page.
Routes are kind of “magic”, in the sense that you’re not calling them yourself in your code, but the framework will use them predictably. Thankfully, Gracile isn’t crowding top level module exports, but just the default one.
While it adds a level of indentation, it avoids clashes with your module-scoped functions.
This is a perfectly reasonable use of ESM default exports.
No static analysis or extraction either, meaning your functions are not in silos and won’t behave in unexpected ways due to custom pre-processing.

Client-side routing

For now, Gracile doesn’t provide any CSR mechanism out of the box.
Due to the varying needs among developers, it’s not in the scope, but at one point, if it adds value (like client/server symbiosis) an add-on could be created.

Note that the Metadata add-on provides a viewTransition option that will make this browser native feature quickly available to you (it is just a meta tag), but it’s not supported outside Blink-based browsers. It can be a nice progressive enhancement though, but not quite the SPA feel you could get with user-land solutions.

Fortunately, there are plenty of options regarding CSR in the Lit ecosystem:

You might want to try DOM-diffing libraries, too.

Miscellaneous

Trailing slashes

For simplicity and predictability, Gracile is only supporting routes that ends with a slash.
This is for pages and server endpoints. Flexibility will be added for the latter.

Note

The explanation below is extracted from the Rocket web framework documentation.

Below is a summary of investigations by Zach Leatherman and Sebastien Lorber

Legend:

about.html about/index.html
Host /about /about/ /about /about/
GitHub Pages 🆘 404 ➡️ /about/
Netlify ➡️ /about ➡️ /about/
Vercel 🆘 404 🆘 404 🟡💔
Cloudflare Pages ➡️ /about ➡️ /about/
Render 🟡💔 🟡💔
Azure Static Web Apps 🆘 404 🟡💔

If you wanna know more be sure to checkout Trailing Slashes on URLs: Contentious or Settled?.