-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
231 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export { useDocument } | ||
|
||
import type { Document } from '../types/Document.js' | ||
import type { DocumentSetter } from './useDocument-types.js' | ||
|
||
function useDocument(): DocumentSetter { | ||
return (document: Document) => { | ||
{ | ||
const { title } = document | ||
if (title) document.title = 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,31 @@ | ||
export { useDocument } | ||
|
||
import { assert } from '../utils/assert.js' | ||
import { usePageContext } from './usePageContext.js' | ||
import { useStream } from 'react-streaming' | ||
import type { Document } from '../types/Document.js' | ||
import type { DocumentSetter } from './useDocument-types.js' | ||
|
||
function useDocument(): DocumentSetter { | ||
const pageContext = usePageContext() | ||
const stream = useStream() | ||
return (document: Document) => { | ||
const htmlHeadAlreadySet: boolean | undefined = (pageContext as any)._htmlHeadAlreadySet | ||
if (htmlHeadAlreadySet === false) { | ||
;(pageContext as any)._document = document | ||
} else { | ||
assert(htmlHeadAlreadySet === true) | ||
assert(stream) | ||
{ | ||
const { title } = document | ||
if (title) { | ||
assert(typeof title === 'string') | ||
// JSON is safe | ||
const titleSafe = JSON.stringify(title) | ||
const htmlSnippet = `<script>document.title = ${titleSafe}</script>` | ||
stream.injectToStream(htmlSnippet) | ||
} | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
export type DocumentSetter = (document: Document) => void |
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,19 @@ | ||
export { getDocument } | ||
|
||
import type { PageContext } from 'vike/types' | ||
import type { Document } from '../types/Document.js' | ||
|
||
function getDocument(pageContext: PageContext): Document { | ||
/* TODO: Vike namespace ConfigResolved | ||
pageContext.config.document | ||
*/ | ||
const values = pageContext.from.configsCumulative.document?.values ?? [] | ||
const document: Document = {} | ||
values.forEach((v) => { | ||
const val = | ||
// We don't do proper assertUsage() validation, but we just type case instead in order to save client-side KBs. | ||
v.value as Document | ||
Object.assign(document, val) | ||
}) | ||
return document | ||
} |
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
export type { Document } | ||
|
||
// TODO: | ||
// - viewport default | ||
// - locale default | ||
// - <meta property="og:type" content="website"> | ||
// - icon large, small, ... | ||
// - preview images with different sizes? | ||
// - dir? https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/dir | ||
// - Can lang be set to en-US instead of en? | ||
type Document = { | ||
title?: string | ||
description?: string | ||
viewport?: string | null | ||
/** | ||
* | ||
* <html lang="en"> | ||
* <meta property="og:locale" content="en_GB"> | ||
* | ||
*/ | ||
locale?: string | ||
|
||
htmlTagAttributes?: Record<string, string> | ||
bodyTagAttributes?: Record<string, string> | ||
rootTagAttributes?: Record<string, string> | ||
|
||
/** Website icon (aka favicon). | ||
* | ||
* <link rel="icon" href="/favicon.ico"> | ||
* <link rel="apple-touch-icon" href="/apple-touch-icon.png"> | ||
*/ | ||
icon?: | ||
| string | ||
| { | ||
url: string | ||
sizes?: string | ||
apple?: boolean | ||
}[] | ||
|
||
/** | ||
* Preview image for social sharing. | ||
* | ||
* <meta property="og:image" content="https://vitejs.dev/og-image.png"> | ||
* <meta name="twitter:card" content="summary_large_image"> | ||
*/ | ||
previewImage?: | ||
| string | ||
| { | ||
url: string | ||
// <meta name="twitter:card" content="summary_large_image"> | ||
size?: 'summary_large_image' | '??' | ||
alt?: string | ||
} | ||
|
||
// <meta name="twitter:site" content="@vite_js"> | ||
twitter?: string | ||
|
||
/** | ||
* Canonical URL. | ||
* | ||
* <meta property="og:url" content="https://www.mywebsite.com/page"> | ||
* <link rel="canonical" href="https://www.mywebsite.com/page"> | ||
*/ | ||
canonical?: string | ||
|
||
/** | ||
* The web manifest URL. | ||
* | ||
* `<link rel="manifest" href="${document.manifest}">` | ||
* | ||
* https://developer.mozilla.org/en-US/docs/Web/Manifest | ||
* https://vike.dev/document | ||
*/ | ||
manifest?: string | ||
|
||
/** | ||
* The UI frame color for mobile. | ||
* | ||
* `<meta name="theme-color" content="${document.themeColor}">` | ||
* | ||
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name/theme-color | ||
* https://vike.dev/document | ||
*/ | ||
themeColor?: | ||
| string | ||
| { | ||
/** | ||
* `<meta name="theme-color" content="${document.themeColor.dark}" media="(prefers-color-scheme: dark)">` | ||
* | ||
* https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme | ||
* https://vike.dev/document | ||
*/ | ||
dark: string | ||
/** | ||
* `<meta name="theme-color" content="${document.themeColor.light}" media="(prefers-color-scheme: light)">` | ||
* | ||
* https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme | ||
* https://vike.dev/document | ||
*/ | ||
light: string | ||
} | ||
} |
Oops, something went wrong.