-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from vikejs/aurelien/bugfix/head-management
fix: simplify `<head>` management
- Loading branch information
Showing
46 changed files
with
159 additions
and
876 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,24 @@ | ||
// https://vike.dev/data | ||
export { data }; | ||
export type { Data }; | ||
export type Data = Awaited<ReturnType<typeof data>>; | ||
|
||
import fetch from "cross-fetch"; | ||
import fetch from "node-fetch"; | ||
import type { PageContextServer } from "vike/types"; | ||
import { filterMovieData } from "../filterMovieData"; | ||
import type { MovieDetails } from "../types"; | ||
|
||
type Data = Awaited<ReturnType<typeof data>>; | ||
|
||
const data = async (pageContext: PageContextServer) => { | ||
const response = await fetch( | ||
`https://star-wars.brillout.com/api/films/${pageContext.routeParams?.id}.json` | ||
`https://brillout.github.io/star-wars/api/films/${pageContext.routeParams?.id}.json` | ||
); | ||
let movie = (await response.json()) as MovieDetails; | ||
|
||
// We remove data we don't need because we pass `pageContext.movie` to | ||
// the client; we want to minimize what is sent over the network. | ||
movie = filterMovieData(movie); | ||
|
||
const { title } = movie; | ||
|
||
return { | ||
movie, | ||
// The page's <title> | ||
title, | ||
}; | ||
movie = minimize(movie); | ||
return movie; | ||
}; | ||
|
||
function minimize(movie: MovieDetails): MovieDetails { | ||
const { id, title, release_date, director, producer } = movie; | ||
movie = { id, title, release_date, director, producer }; | ||
return movie; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export { title }; | ||
|
||
import type { Data } from "./+data"; | ||
import type { PageContext } from "vike/types"; | ||
|
||
function title(pageContext: PageContext<Data>) { | ||
const movie = pageContext.data; | ||
return movie.title; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,24 @@ | ||
// https://vike.dev/data | ||
export { data }; | ||
export type { Data }; | ||
export type Data = Awaited<ReturnType<typeof data>>; | ||
|
||
import fetch from "node-fetch"; | ||
//import { filterMovieData } from '../filterMovieData' | ||
import type { Movie, MovieDetails } from "../types"; | ||
|
||
// export { onBeforePrerenderStart } | ||
|
||
type Data = Awaited<ReturnType<typeof data>>; | ||
|
||
const data = async () => { | ||
const movies = await getStarWarsMovies(); | ||
return { | ||
// We remove data we don't need because we pass `pageContext.movies` to | ||
// the client; we want to minimize what is sent over the network. | ||
movies: filterMoviesData(movies), | ||
// The page's <title> | ||
title: getTitle(movies), | ||
}; | ||
}; | ||
|
||
async function getStarWarsMovies(): Promise<MovieDetails[]> { | ||
const response = await fetch("https://star-wars.brillout.com/api/films.json"); | ||
let movies: MovieDetails[] = ((await response.json()) as any).results; | ||
movies = movies.map((movie: MovieDetails, i: number) => ({ | ||
...movie, | ||
id: String(i + 1), | ||
})); | ||
const response = await fetch( | ||
"https://brillout.github.io/star-wars/api/films.json" | ||
); | ||
const moviesData = (await response.json()) as MovieDetails[]; | ||
// We remove data we don't need because the data is passed to the client; we should | ||
// minimize what is sent over the network. | ||
const movies = minimize(moviesData); | ||
return movies; | ||
} | ||
}; | ||
|
||
function filterMoviesData(movies: MovieDetails[]): Movie[] { | ||
return movies.map((movie: MovieDetails) => { | ||
function minimize(movies: MovieDetails[]): Movie[] { | ||
return movies.map((movie) => { | ||
const { title, release_date, id } = movie; | ||
return { title, release_date, id }; | ||
}); | ||
} | ||
|
||
/* | ||
async function onBeforePrerenderStart() { | ||
const movies = await getStarWarsMovies() | ||
return [ | ||
{ | ||
url: '/star-wars', | ||
// We already provide `pageContext` here so that vike-solid | ||
// will *not* have to call the `data()` hook defined | ||
// above in this file. | ||
pageContext: { | ||
data: { | ||
movies: filterMoviesData(movies), | ||
title: getTitle(movies) | ||
}, | ||
} | ||
}, | ||
...movies.map((movie) => { | ||
const url = `/star-wars/${movie.id}` | ||
return { | ||
url, | ||
// Note that we can also provide the `pageContext` of other pages. | ||
// This means that vike-solid will not call any | ||
// `data()` hook and the Star Wars API will be called | ||
// only once (in this `onBeforePrerenderStart()` hook). | ||
pageContext: { | ||
data: { | ||
movie: filterMovieData(movie), | ||
title: movie.title | ||
}, | ||
} | ||
} | ||
}) | ||
] | ||
} | ||
*/ | ||
|
||
function getTitle(movies: Movie[] | MovieDetails[]): string { | ||
const title = `${movies.length} Star Wars Movies`; | ||
return title; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export { title }; | ||
|
||
import type { Data } from "./+data"; | ||
import type { PageContext } from "vike/types"; | ||
|
||
function title(pageContext: PageContext<Data>) { | ||
const movies = pageContext.data; | ||
return `${movies.length} Star Wars Movies`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.