forked from withastro/starlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for SSR (withastro#1255)
Co-authored-by: Patrick Jakubik <37963339+patryk-smc@users.noreply.github.com> Co-authored-by: Roni Costa <622159+ronildo@users.noreply.github.com> Co-authored-by: Ryan Russell <523300+ryanrussell@users.noreply.github.com> Co-authored-by: HiDeoo <494699+HiDeoo@users.noreply.github.com> Co-authored-by: Murali Manohar Varma <67296473+mmv-dev@users.noreply.github.com> Co-authored-by: Marcelo Cardoso <33183880+marcelovicentegc@users.noreply.github.com> Co-authored-by: Aaron Siddhartha Mondal <28633256+aaronmondal@users.noreply.github.com> Co-authored-by: Chris Swithinbank <357379+delucis@users.noreply.github.com> Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
- Loading branch information
1 parent
20cbf3b
commit 6f3202b
Showing
48 changed files
with
1,004 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@astrojs/starlight': patch | ||
--- | ||
|
||
Improves performance of computing the last updated times from Git history. | ||
|
||
Instead of executing `git` for each docs page, it is now executed twice regardless of the number of pages. |
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,5 @@ | ||
--- | ||
'@astrojs/starlight': patch | ||
--- | ||
|
||
Fixes last updated times on projects with custom `srcDir` |
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,16 @@ | ||
--- | ||
'@astrojs/starlight': minor | ||
--- | ||
|
||
Adds support for server-rendered Starlight pages. | ||
|
||
When building a project with `hybrid` or `server` output mode, a new `prerender` option on Starlight config can be set to `false` to make all Starlight pages be rendered on-demand: | ||
|
||
```ts | ||
export default defineConfig({ | ||
output: 'server', | ||
integrations: [starlight({ | ||
prerender: false | ||
})], | ||
}) | ||
``` |
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,4 +1,4 @@ | ||
# Astro generates this during tests, but we want to ignore it. | ||
src/env.d.ts | ||
__tests__/**/env.d.ts | ||
env.d.ts | ||
__tests__/**/types.d.ts | ||
.astro |
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,10 @@ | ||
# This fixture is used for the E2E tests related to Git where | ||
# a new repo is created. Nested repos do not inherit the .gitignore | ||
# options from the parent repo, so even though these files are | ||
# already ignored on the package they have to be re-declared here. | ||
# Do not delete this file or the Git tests will try to index and | ||
# and commit all the node_modules and generated files from the test. | ||
env.d.ts | ||
.astro | ||
/node_modules/ | ||
/dist/ |
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,11 @@ | ||
import starlight from '@astrojs/starlight'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
integrations: [ | ||
starlight({ | ||
title: 'Git', | ||
pagefind: false, | ||
}), | ||
], | ||
}); |
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 @@ | ||
{ | ||
"name": "@e2e/git", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"@astrojs/starlight": "workspace:*", | ||
"astro": "^4.15.3" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/starlight/__e2e__/fixtures/git/src/content/config.ts
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,6 @@ | ||
import { defineCollection } from 'astro:content'; | ||
import { docsSchema } from '@astrojs/starlight/schema'; | ||
|
||
export const collections = { | ||
docs: defineCollection({ schema: docsSchema() }), | ||
}; |
6 changes: 6 additions & 0 deletions
6
packages/starlight/__e2e__/fixtures/git/src/content/docs/index.md
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,6 @@ | ||
--- | ||
title: Home Page | ||
lastUpdated: true | ||
--- | ||
|
||
Home page content |
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,25 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import starlight from '@astrojs/starlight'; | ||
import node from '@astrojs/node'; | ||
|
||
const prerendering = process.env.STARLIGHT_PRERENDER === 'yes'; | ||
|
||
export default defineConfig({ | ||
output: 'server', | ||
adapter: node({ mode: 'standalone' }), | ||
compressHTML: false, // for easier debugging | ||
// Output to different folders and expose on different ports | ||
// on each case so the servers don't conflict with | ||
// each other during tests. | ||
outDir: prerendering ? 'dist' : 'build', | ||
server: { | ||
port: prerendering ? 4322 : 4321, | ||
}, | ||
integrations: [ | ||
starlight({ | ||
title: 'SSR', | ||
prerender: prerendering, | ||
pagefind: false, | ||
}), | ||
], | ||
}); |
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,10 @@ | ||
{ | ||
"name": "@e2e/ssr", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"@astrojs/node": "^8.3.2", | ||
"@astrojs/starlight": "workspace:*", | ||
"astro": "^4.15.3" | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/starlight/__e2e__/fixtures/ssr/src/component/ServerCheck.astro
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,15 @@ | ||
--- | ||
function checkServer() { | ||
try { | ||
Astro.clientAddress; | ||
return true; | ||
} catch { | ||
// Accessing `clientAddress` during build time | ||
// fails, so it is not SSR. | ||
return false; | ||
} | ||
} | ||
--- | ||
|
||
<div id="server-check">{checkServer() ? 'On server' : 'Not server'}</div> |
6 changes: 6 additions & 0 deletions
6
packages/starlight/__e2e__/fixtures/ssr/src/content/config.ts
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,6 @@ | ||
import { defineCollection } from 'astro:content'; | ||
import { docsSchema } from '@astrojs/starlight/schema'; | ||
|
||
export const collections = { | ||
docs: defineCollection({ schema: docsSchema() }), | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/starlight/__e2e__/fixtures/ssr/src/content/docs/404.mdx
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 @@ | ||
--- | ||
title: Not Found | ||
template: splash | ||
lastUpdate: true | ||
--- | ||
|
||
import ServerCheck from '../../component/ServerCheck.astro'; | ||
|
||
<ServerCheck /> |
6 changes: 6 additions & 0 deletions
6
packages/starlight/__e2e__/fixtures/ssr/src/content/docs/content.mdx
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,6 @@ | ||
--- | ||
title: Content | ||
lastUpdate: true | ||
--- | ||
|
||
Example page |
8 changes: 8 additions & 0 deletions
8
packages/starlight/__e2e__/fixtures/ssr/src/content/docs/demo.mdx
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,8 @@ | ||
--- | ||
title: Server Check | ||
lastUpdate: true | ||
--- | ||
|
||
import ServerCheck from '../../component/ServerCheck.astro'; | ||
|
||
<ServerCheck /> |
18 changes: 18 additions & 0 deletions
18
packages/starlight/__e2e__/fixtures/ssr/src/content/docs/index.md
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,18 @@ | ||
--- | ||
title: Home Page | ||
template: splash | ||
lastUpdate: true | ||
hero: | ||
title: Make your docs shine with Starlight | ||
tagline: Everything you need to build a stellar documentation website. Fast, accessible, and easy-to-use. | ||
actions: | ||
- text: Get started | ||
icon: right-arrow | ||
variant: primary | ||
link: /getting-started/ | ||
- text: View on GitHub | ||
icon: external | ||
link: https://github.com/withastro/starlight | ||
--- | ||
|
||
Home page content |
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,36 @@ | ||
import { makeTestRepo } from '../__tests__/git-utils'; | ||
import { expect, testFactory } from './test-utils'; | ||
import { rm } from 'node:fs/promises'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { join } from 'node:path'; | ||
|
||
const repoPath = fileURLToPath(new URL('./fixtures/git/', import.meta.url)); | ||
|
||
const test = testFactory(repoPath); | ||
|
||
test.beforeAll(async () => { | ||
// Clears existing nested repo to account for previously interrupted tests. | ||
await rm(join(repoPath, '.git'), { recursive: true, force: true }); | ||
const testRepo = makeTestRepo(repoPath); | ||
|
||
testRepo.commitAllChanges('Add home page', '2024-02-03'); | ||
}); | ||
|
||
test.afterAll(async () => { | ||
// Remove nested repo after test runs | ||
await rm(join(repoPath, '.git'), { recursive: true, force: true }); | ||
}); | ||
|
||
test('include last updated date from git in the footer', async ({ page, getProdServer }) => { | ||
const starlight = await getProdServer(); | ||
await starlight.goto('/'); | ||
|
||
await expect(page.locator('footer')).toContainText('Last updated: Feb 3, 2024'); | ||
}); | ||
|
||
test('include git information while developing', async ({ page, makeServer }) => { | ||
const starlight = await makeServer('dev', { mode: 'dev' }); | ||
await starlight.goto('/'); | ||
|
||
await expect(page.locator('footer')).toContainText('Last updated: Feb 3, 2024'); | ||
}); |
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,66 @@ | ||
import { expect, testFactory } from './test-utils'; | ||
import assert from 'node:assert'; | ||
import { parseHTML } from 'linkedom'; | ||
|
||
const test = testFactory('./fixtures/ssr/'); | ||
|
||
test.beforeEach(() => { | ||
delete process.env.STARLIGHT_PRERENDER; | ||
}); | ||
|
||
test('Render page on the server', async ({ page, getProdServer }) => { | ||
const starlight = await getProdServer(); | ||
await starlight.goto('/demo'); | ||
|
||
await expect(page.locator('#server-check')).toHaveText('On server'); | ||
}); | ||
|
||
test('Render 404 page on the server', async ({ page, getProdServer }) => { | ||
const starlight = await getProdServer(); | ||
await starlight.goto('/not-found'); | ||
|
||
await expect(page.locator('#server-check')).toHaveText('On server'); | ||
}); | ||
|
||
test('SSR mode renders the same content page as prerendering', async ({ | ||
getProdServer, | ||
makeServer, | ||
}) => { | ||
const starlight = await getProdServer(); | ||
const ssrContent = await starlight.goto('/content').then((res) => res?.text()); | ||
assert(ssrContent); | ||
|
||
process.env.STARLIGHT_PRERENDER = 'yes'; | ||
const prerenderStarlight = await makeServer('prerender'); | ||
const prerenderContent = await prerenderStarlight.goto('/content').then((res) => res?.text()); | ||
assert(prerenderContent); | ||
|
||
expectEquivalentHTML(prerenderContent, ssrContent); | ||
}); | ||
|
||
test('SSR mode renders the same splash page as prerendering', async ({ | ||
getProdServer, | ||
makeServer, | ||
}) => { | ||
const starlight = await getProdServer(); | ||
const ssrContent = await starlight.goto('/').then((res) => res?.text()); | ||
assert(ssrContent); | ||
|
||
process.env.STARLIGHT_PRERENDER = 'yes'; | ||
const prerenderStarlight = await makeServer('prerender'); | ||
const prerenderContent = await prerenderStarlight.goto('/').then((res) => res?.text()); | ||
assert(prerenderContent); | ||
|
||
expectEquivalentHTML(prerenderContent, ssrContent); | ||
}); | ||
|
||
function expectEquivalentHTML(a: string, b: string) { | ||
expect(getNormalizedHTML(a)).toEqual(getNormalizedHTML(b)); | ||
} | ||
|
||
function getNormalizedHTML(html: string) { | ||
const window = parseHTML(html); | ||
window.document.querySelectorAll('script[src]').forEach((el) => el.setAttribute('src', '')); | ||
window.document.querySelectorAll('link[href]').forEach((el) => el.setAttribute('href', '')); | ||
return window.toString(); | ||
} |
Oops, something went wrong.