Skip to content

Commit

Permalink
fix: simplify <head> management
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Fetching data using `pageContext.pageProps` is
deprecated, use `data()` and `useData()` instead,
see https://vike.dev/data-fetching

BREAKING CHANGE: Setting the page's title using `pageContext.title`
is deprecated (`pageContext.description` and `pageContext.lang` are
also deprecated), use the settings `title` and `Head` instead,
see https://vike.dev/head
  • Loading branch information
lourot committed Jan 22, 2024
1 parent 45fa236 commit 39aa1a9
Show file tree
Hide file tree
Showing 66 changed files with 195 additions and 903 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jobs:
os: [ubuntu-latest, windows-latest, macos-latest]
example:
- examples/basic
- examples/onBeforeRender
- examples/ssr-spa
- examples/with-vue-plugin
- examples/with-vike-pinia
Expand Down
6 changes: 6 additions & 0 deletions examples/basic/layouts/HeadDefault.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<template>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Demo showcasing Vike + Vue" />
<link rel="icon" :href="logoUrl" />
</template>

<script lang="ts" setup>
import logoUrl from '../assets/logo.svg'
</script>
3 changes: 1 addition & 2 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
"@types/node": "^20.11.1",
"@types/node-fetch": "^2.6.10",
"@vitejs/plugin-vue": "^5.0.3",
"cross-fetch": "^4.0.0",
"node-fetch": "^3.3.2",
"typescript": "^5.3.3",
"vike-vue": "0.5.4",
"vite": "^5.0.11",
"unplugin-vue-markdown": "^0.25.2",
"vike": "0.4.156",
"vike": "0.4.159",
"vue": "^3.4.13"
},
"type": "module"
Expand Down
5 changes: 0 additions & 5 deletions examples/basic/pages/+config.h.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Config } from 'vike/types'
import logoUrl from '../assets/logo.svg'
import Layout from '../layouts/LayoutDefault.vue'
import Head from '../layouts/HeadDefault.vue'
import vikeVue from 'vike-vue'
Expand All @@ -10,9 +9,5 @@ export default {
Head,
// <title>
title: 'My Vike + Vue App',
// <meta name="description">
description: 'Demo showcasing Vike + Vue',
// <link rel="icon" href="${favicon}" />
favicon: logoUrl,
extends: vikeVue
} satisfies Config
9 changes: 0 additions & 9 deletions examples/basic/pages/star-wars/filterMovieData.ts

This file was deleted.

6 changes: 3 additions & 3 deletions examples/basic/pages/star-wars/index/+Page.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<template>
<h1>Star Wars Movies</h1>
<ol>
<li v-for="item in data.movies" :key="item.id">
<li v-for="item in movies" :key="item.id">
<a :href="'/star-wars/' + item.id">{{ item.title }}</a> ({{ item.release_date }})
</li>
</ol>
<p>Source: <a href="https://star-wars.brillout.com">star-wars.brillout.com</a>.</p>
<p>Source: <a href="https://brillout.github.io/star-wars">brillout.github.io/star-wars</a>.</p>
<p>Data can be fetched by using the <code>data()</code> hook.</p>
</template>

<script lang="ts" setup>
import type { Data } from './+data'
import { useData } from 'vike-vue/useData'
const data = useData<Data>()
const movies = useData<Data>()
</script>
28 changes: 16 additions & 12 deletions examples/basic/pages/star-wars/index/+data.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// https://vike.dev/data
export { data }
export type { Data }
export type Data = Awaited<ReturnType<typeof data>>

import { fetchStarWarsMovies, filterMoviesData, getTitle } from './data'

type Data = Awaited<ReturnType<typeof data>>
import fetch from 'node-fetch'
import type { Movie, MovieDetails } from '../types'

const data = async () => {
const movies = await fetchStarWarsMovies()
return {
// We remove data we don't need because the data is passed to the client; we should
// minimize what is sent over the network.
movies: filterMoviesData(movies),
// The page's <title>
title: getTitle(movies)
}
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 minimize(movies: MovieDetails[]): Movie[] {
return movies.map((movie) => {
const { title, release_date, id } = movie
return { title, release_date, id }
})
}
47 changes: 0 additions & 47 deletions examples/basic/pages/star-wars/index/+onBeforePrerenderStart.ts

This file was deleted.

9 changes: 9 additions & 0 deletions examples/basic/pages/star-wars/index/+title.ts
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`
}
26 changes: 0 additions & 26 deletions examples/basic/pages/star-wars/index/data.ts

This file was deleted.

10 changes: 5 additions & 5 deletions examples/basic/pages/star-wars/movie/+Page.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<template>
<h1>{{ data.movie.title }}</h1>
Release Date: {{ data.movie.release_date }}
<h1>{{ movie.title }}</h1>
Release Date: {{ movie.release_date }}
<br />
Director: {{ data.movie.director }}
Director: {{ movie.director }}
<br />
Producer: {{ data.movie.producer }}
Producer: {{ movie.producer }}
</template>

<script lang="ts" setup>
import type { Data } from './+data'
import { useData } from 'vike-vue/useData'
const data = useData<Data>()
const movie = useData<Data>()
</script>
28 changes: 12 additions & 16 deletions examples/basic/pages/star-wars/movie/+data.ts
Original file line number Diff line number Diff line change
@@ -1,28 +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?.movieId}.json`)
const response = await fetch(
`https://brillout.github.io/star-wars/api/films/${pageContext.routeParams?.movieId}.json`
)
let movie = (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.
movie = filterMovieData(movie)

// The page's <title>
const { title } = movie
movie = minimize(movie)
return movie
}

return {
movie,
// The page's <title>
title
}
function minimize(movie: MovieDetails): MovieDetails {
const { id, title, release_date, director, producer } = movie
movie = { id, title, release_date, director, producer }
return movie
}
9 changes: 9 additions & 0 deletions examples/basic/pages/star-wars/movie/+title.ts
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
}
5 changes: 1 addition & 4 deletions examples/basic/pages/star-wars/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ export type Movie = {
title: string
release_date: string
}
export type MovieDetails = {
id: string
title: string
release_date: string
export type MovieDetails = Movie & {
director: string
producer: string
}
5 changes: 2 additions & 3 deletions examples/basic/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import { UserConfig } from 'vite'

const config: UserConfig = {
plugins: [
vike({ prerender: true }),
vike(),
vue({
include: [/\.vue$/, /\.md$/]
}),
md({})
],
optimizeDeps: { include: ['cross-fetch'] }
]
}

export default config
2 changes: 0 additions & 2 deletions examples/onBeforeRender/.gitignore

This file was deleted.

36 changes: 0 additions & 36 deletions examples/onBeforeRender/assets/logo.svg

This file was deleted.

15 changes: 0 additions & 15 deletions examples/onBeforeRender/components/Counter.vue

This file was deleted.

25 changes: 0 additions & 25 deletions examples/onBeforeRender/components/Link.vue

This file was deleted.

3 changes: 0 additions & 3 deletions examples/onBeforeRender/layouts/HeadDefault.vue

This file was deleted.

Loading

0 comments on commit 39aa1a9

Please sign in to comment.