source: pages/star-wars/@id/+data.ts@ fdd776b

main
Last change on this file since fdd776b was 8ee4553, checked in by Bati <no-reply@…>, 7 months ago

scaffold Vike app with Bati

  • Property mode set to 100644
File size: 953 bytes
Line 
1// https://vike.dev/data
2
3import type { PageContextServer } from "vike/types";
4import { useConfig } from "vike-react/useConfig";
5import type { MovieDetails } from "../types.js";
6
7export type Data = Awaited<ReturnType<typeof data>>;
8
9export async function data(pageContext: PageContextServer) {
10 // https://vike.dev/useConfig
11 const config = useConfig();
12
13 const response = await fetch(`https://brillout.github.io/star-wars/api/films/${pageContext.routeParams.id}.json`);
14 let movie = (await response.json()) as MovieDetails;
15
16 config({
17 // Set <title>
18 title: movie.title,
19 });
20
21 // We remove data we don't need because the data is passed to
22 // the client; we should minimize what is sent over the network.
23 movie = minimize(movie);
24
25 return { movie };
26}
27
28function minimize(movie: MovieDetails): MovieDetails {
29 const { id, title, release_date, director, producer } = movie;
30 return { id, title, release_date, director, producer };
31}
Note: See TracBrowser for help on using the repository browser.