Skip to content

Add a page ​

This tutorial adds a page end to end: the API access function, the page, the link, the tests and the docs.

A made-up example

We add a /ruptures page listing out-of-stock products. This page does not exist in the project: it is only an example. Replace it with your real page.

1. The API access function ​

Every API call goes in web/lib/products.js. Here, no new endpoint is needed: we reuse fetchProducts() and filter.

js
// web/lib/products.js
export async function fetchOutOfStock() {
  const products = await fetchProducts();
  return products.filter((product) => product.stock === 0);
}

If your page needs a new endpoint, it must first exist in the API: see Add a route.

2. The page ​

A folder in app/ = an address. Create web/app/ruptures/page.js:

jsx
import ProductList from '../../components/ProductList';
import { fetchOutOfStock } from '../../lib/products';

export const dynamic = 'force-dynamic';

export default async function OutOfStockPage() {
  let products = [];
  let error = null;

  try {
    products = await fetchOutOfStock();
  } catch (e) {
    error = e.message;
  }

  return (
    <main>
      <h1>Ruptures</h1>
      {error ? <p role="alert" className="alert">API indisponible : {error}</p> : <ProductList products={products} />}
    </main>
  );
}

Do not forget force-dynamic: without it, the pipeline build fails. See Server rendering and state.

Add a link in the header of web/app/layout.js, with next/link:

jsx
import Link from 'next/link';
// …
<Link href="/ruptures">Ruptures</Link>

4. The tests ​

A test for the function in __tests__/products.test.js, and a __tests__/ruptures.test.js file for the page, modelled on page.test.js:

js
import { render, screen } from '@testing-library/react';
import OutOfStockPage from '../app/ruptures/page';
import { fetchOutOfStock } from '../lib/products';

jest.mock('../lib/products');
jest.mock('../app/actions', () => ({ removeProduct: jest.fn() }));

it('affiche les produits en rupture', async () => {
  fetchOutOfStock.mockResolvedValue([{ id: 1, name: 'Beurre', price: '3', stock: 0 }]);

  render(await OutOfStockPage());

  expect(screen.getByText('Beurre')).toBeInTheDocument();
});

Run npm run lint and npm test before pushing.

5. The docs ​

Update, in the same merge request:

  • Products or a new page under Features, with the page's rules;
  • Folder layout;
  • the sidebar in docs/.vitepress/config.mts if you add a docs page, in French and in English.

6. The merge request ​

Push to a feature/ruptures branch and open a merge request to develop. The pipeline runs web:lint and web:test. Once merged, the page goes to dev.

Final checklist ​

If you add…Also update…
an API calllib/products.js + __tests__/products.test.js + Talking to the API
a pagea test file + Folder layout + a Features page
an environment variableweb/.env.example + infra/docker-compose.*.yml + Reference
a librarypackage.json and package-lock.json (with npm install) + Reference
a docs pageconfig.mts, in French and in English