Skip to content

Commit

Permalink
rename contextProps to pageContext, and addContextProps to `add…
Browse files Browse the repository at this point in the history
…PageContext` (fix #58)

BREAKING CHANGE: Replace all occurences in your source code of `addContextProps` to
`addPageContext`, and all occurences of `contextProps` to `pageContext`.
There is no need for semantic replacing: you can simply replace
text, for example with a linux terminal:
 - `git ls-files | xargs sed -i "s/addContextProps/addPageContext/g"`
 - `git ls-files | xargs sed -i "s/contextProps/pageContext/g"`
  • Loading branch information
brillout committed May 25, 2021
1 parent b6b0d45 commit aedf9fc
Show file tree
Hide file tree
Showing 74 changed files with 567 additions and 569 deletions.
358 changes: 178 additions & 180 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { PageLayout } from "./PageLayout";
hydrate();

async function hydrate() {
const { Page, contextProps } = await getPage();
const { Page, pageContext } = await getPage();

ReactDOM.hydrate(
<PageLayout>
<Page {...contextProps.pageProps} />
<Page {...pageContext.pageProps} />
</PageLayout>,
document.getElementById("page-view")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ReactDOMServer from "react-dom/server";
import React from "react";
import { PageLayout } from "./PageLayout";
import { html } from "vite-plugin-ssr";
import { ContextProps, ReactComponent } from "./types";
import { PageContext, ReactComponent } from "./types";
import logoUrl from "./logo.svg";

export { render };
Expand All @@ -13,14 +13,14 @@ const passToClient = ["pageProps"];

function render({
Page,
contextProps,
pageContext,
}: {
Page: ReactComponent;
contextProps: ContextProps;
pageContext: PageContext;
}) {
const pageHtml = ReactDOMServer.renderToString(
<PageLayout>
<Page {...contextProps.pageProps} />
<Page {...pageContext.pageProps} />
</PageLayout>
);
const title = "Vite SSR app";
Expand Down
2 changes: 1 addition & 1 deletion boilerplates/boilerplate-react-ts/pages/_default/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type ReactComponent = (pageProps: PageProps) => JSX.Element;
export type PageProps = {};
export type ContextProps = {
export type PageContext = {
pageProps: PageProps;
};
4 changes: 2 additions & 2 deletions boilerplates/boilerplate-react-ts/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ async function startServer() {
const renderPage = createPageRender({ viteDevServer, isProduction, root });
app.get("*", async (req, res, next) => {
const url = req.originalUrl;
const contextProps = {};
const result = await renderPage({ url, contextProps });
const pageContext = {};
const result = await renderPage({ url, pageContext });
if (result.nothingRendered) return next();
res.status(result.statusCode).send(result.renderResult);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { PageLayout } from "./PageLayout";
hydrate();

async function hydrate() {
const { Page, contextProps } = await getPage();
const { Page, pageContext } = await getPage();

ReactDOM.hydrate(
<PageLayout>
<Page {...contextProps.pageProps} />
<Page {...pageContext.pageProps} />
</PageLayout>,
document.getElementById("page-view")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export { passToClient };
// See https://github.com/brillout/vite-plugin-ssr#data-fetching
const passToClient = ["pageProps"];

function render({ Page, contextProps }) {
function render({ Page, pageContext }) {
const pageHtml = ReactDOMServer.renderToString(
<PageLayout>
<Page {...contextProps.pageProps} />
<Page {...pageContext.pageProps} />
</PageLayout>
);
const title = "Vite SSR app";
Expand Down
4 changes: 2 additions & 2 deletions boilerplates/boilerplate-react/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ async function startServer() {
const renderPage = createPageRender({ viteDevServer, isProduction, root });
app.get("*", async (req, res, next) => {
const url = req.originalUrl;
const contextProps = {};
const result = await renderPage({ url, contextProps });
const pageContext = {};
const result = await renderPage({ url, pageContext });
if (result.nothingRendered) return next();
res.status(result.statusCode).send(result.renderResult);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createApp } from './app'
hydrate()

async function hydrate() {
const { Page, contextProps } = await getPage()
const app = createApp(Page, contextProps)
const { Page, pageContext } = await getPage()
const app = createApp(Page, pageContext)
app.mount('#app')
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { renderToString } from '@vue/server-renderer'
import { html } from 'vite-plugin-ssr'
import { createApp } from './app'
import { ContextProps, VueComponent } from './types'
import { PageContext, VueComponent } from './types'
import logoUrl from './logo.svg'

export { render }
Expand All @@ -10,8 +10,8 @@ export { passToClient }
// See https://github.com/brillout/vite-plugin-ssr#data-fetching
const passToClient = ['pageProps', 'routeParams']

async function render({ Page, contextProps }: { Page: VueComponent; contextProps: ContextProps }) {
const app = createApp(Page, contextProps)
async function render({ Page, pageContext }: { Page: VueComponent; pageContext: PageContext }) {
const app = createApp(Page, pageContext)
const appHtml = await renderToString(app)
const title = 'Vite SSR app'
const description = 'An app using Vite and vite-plugin-ssr.'
Expand Down
10 changes: 5 additions & 5 deletions boilerplates/boilerplate-vue-ts/pages/_default/app.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { createSSRApp, defineComponent, h } from 'vue'
import PageLayout from './PageLayout.vue'
import { ContextProps } from './types'
import { PageContext } from './types'

export { createApp }

function createApp(Page: any, contextProps: ContextProps) {
function createApp(Page: any, pageContext: PageContext) {
const PageWithLayout = defineComponent({
render() {
return h(
PageLayout,
{},
{
default() {
return h(Page, contextProps.pageProps || {})
return h(Page, pageContext.pageProps || {})
}
}
)
Expand All @@ -21,9 +21,9 @@ function createApp(Page: any, contextProps: ContextProps) {

const app = createSSRApp(PageWithLayout)

// We make `contextProps.routeParams` available in all components as `$routeParams`
// We make `pageContext.routeParams` available in all components as `$routeParams`
// (e.g. `$routeParams.movieId` for a Route String `/movie/:movieId`).
app.config.globalProperties.$routeParams = contextProps.routeParams
app.config.globalProperties.$routeParams = pageContext.routeParams

return app
}
2 changes: 1 addition & 1 deletion boilerplates/boilerplate-vue-ts/pages/_default/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type PageProps = {}
export type ContextProps = {
export type PageContext = {
pageProps?: PageProps
routeParams: Record<string, string>
}
Expand Down
4 changes: 2 additions & 2 deletions boilerplates/boilerplate-vue-ts/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ async function startServer() {
const renderPage = createPageRender({ viteDevServer, isProduction, root })
app.get('*', async (req, res, next) => {
const url = req.originalUrl
const contextProps = {}
const result = await renderPage({ url, contextProps })
const pageContext = {}
const result = await renderPage({ url, pageContext })
if (result.nothingRendered) return next()
res.status(result.statusCode).send(result.renderResult)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createApp } from './app'
hydrate()

async function hydrate() {
const { Page, contextProps } = await getPage()
const app = createApp(Page, contextProps)
const { Page, pageContext } = await getPage()
const app = createApp(Page, pageContext)
app.mount('#app')
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export { passToClient }
// See https://github.com/brillout/vite-plugin-ssr#data-fetching
const passToClient = ['pageProps', 'routeParams']

async function render({ Page, contextProps }) {
const app = createApp(Page, contextProps)
async function render({ Page, pageContext }) {
const app = createApp(Page, pageContext)
const appHtml = await renderToString(app)
const title = 'Vite SSR app'
const description = 'An app using Vite and vite-plugin-ssr.'
Expand Down
8 changes: 4 additions & 4 deletions boilerplates/boilerplate-vue/pages/_default/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import PageLayout from './PageLayout.vue'

export { createApp }

function createApp(Page, contextProps) {
function createApp(Page, pageContext) {
const PageWithLayout = {
render() {
return h(
PageLayout,
{},
{
default() {
return h(Page, contextProps.pageProps || {})
return h(Page, pageContext.pageProps || {})
}
}
)
Expand All @@ -20,9 +20,9 @@ function createApp(Page, contextProps) {

const app = createSSRApp(PageWithLayout)

// We make `contextProps.routeParams` available in all components as `$routeParams`
// We make `pageContext.routeParams` available in all components as `$routeParams`
// (e.g. `$routeParams.movieId` for a Route String `/movie/:movieId`).
app.config.globalProperties.$routeParams = contextProps.routeParams
app.config.globalProperties.$routeParams = pageContext.routeParams

return app
}
4 changes: 2 additions & 2 deletions boilerplates/boilerplate-vue/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ async function startServer() {
const renderPage = createPageRender({ viteDevServer, isProduction, root })
app.get('*', async (req, res, next) => {
const url = req.originalUrl
const contextProps = {}
const result = await renderPage({ url, contextProps })
const pageContext = {}
const result = await renderPage({ url, pageContext })
if (result.nothingRendered) return next()
res.status(result.statusCode).send(result.renderResult)
})
Expand Down
4 changes: 2 additions & 2 deletions examples/.testPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ function testPages(viewFramework: 'vue' | 'react', cmd: 'npm run start' | 'npm r
// The HTML is from the first page before client-side routing
const html = await page.content()
// `page.content()` doesn't return the original HTML (it dumps the DOM to HTML).
// Therefore only the serialized `contextProps` tell us the original HTML.
// Therefore only the serialized `pageContext` tell us the original HTML.
expect(html).toContain(
`<script>window.__vite_plugin_ssr = {pageId: "\\u002Fpages\\u002Findex", contextProps: (function(a){return {pageProps:a,docTitle:a${
`<script>window.__vite_plugin_ssr = {pageId: "\\u002Fpages\\u002Findex", pageContext: (function(a){return {pageProps:a,docTitle:a${
viewFramework === 'vue' ? ',routeParams:{}' : ''
}}}(void 0))}</script>`
)
Expand Down
4 changes: 2 additions & 2 deletions examples/base-url/pages/_default/_default.page.client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { PageLayout } from "./PageLayout";
hydrate();

async function hydrate() {
const { Page, contextProps } = await getPage();
const { Page, pageContext } = await getPage();

ReactDOM.hydrate(
<PageLayout>
<Page {...contextProps.pageProps} />
<Page {...pageContext.pageProps} />
</PageLayout>,
document.getElementById("page-view")
);
Expand Down
4 changes: 2 additions & 2 deletions examples/base-url/pages/_default/_default.page.server.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export { passToClient };

const passToClient = ["pageProps"];

function render({ Page, contextProps }) {
function render({ Page, pageContext }) {
const pageHtml = ReactDOMServer.renderToString(
<PageLayout>
<Page {...contextProps.pageProps} />
<Page {...pageContext.pageProps} />
</PageLayout>
);

Expand Down
4 changes: 2 additions & 2 deletions examples/base-url/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ async function startServer() {
});
app.get("*", async (req, res, next) => {
const url = req.originalUrl;
const contextProps = {};
const result = await renderPage({ url, contextProps });
const pageContext = {};
const result = await renderPage({ url, pageContext });
if (result.nothingRendered) return next();
res.status(result.statusCode).send(result.renderResult);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { PageLayout } from "./PageLayout";
hydrate();

async function hydrate() {
const { Page, contextProps } = await getPage();
const { Page, pageContext } = await getPage();

ReactDOM.hydrate(
<PageLayout>
<Page {...contextProps.pageProps} />
<Page {...pageContext.pageProps} />
</PageLayout>,
document.getElementById("page-view")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export { passToClient };

const passToClient = ["pageProps"];

function render({ Page, contextProps }) {
function render({ Page, pageContext }) {
const pageHtml = ReactDOMServer.renderToString(
<PageLayout>
<Page {...contextProps.pageProps} />
<Page {...pageContext.pageProps} />
</PageLayout>
);

Expand Down
4 changes: 2 additions & 2 deletions examples/cloudflare-workers/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ async function startServer() {
const renderPage = createPageRender({ viteDevServer, isProduction, root });
app.get("*", async (req, res, next) => {
const url = req.originalUrl;
const contextProps = {};
const result = await renderPage({ url, contextProps });
const pageContext = {};
const result = await renderPage({ url, pageContext });
if (result.nothingRendered) return next();
res.status(result.statusCode).send(result.renderResult);
});
Expand Down
4 changes: 2 additions & 2 deletions examples/cloudflare-workers/worker/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export { handleSsr };
const renderPage = createPageRender({ isProduction: true });

async function handleSsr(url) {
const contextProps = {};
const result = await renderPage({ url, contextProps });
const pageContext = {};
const result = await renderPage({ url, pageContext });
if (result.nothingRendered) {
return null;
} else {
Expand Down
2 changes: 1 addition & 1 deletion examples/graphql-apollo/.test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test("page is rendered to HTML", async () => {
);
expect(html).toContain("<button>Counter <span>0</span></button>");
expect(html).toMatch(
'<script>window.__vite_plugin_ssr = {pageId: "\\u002Fpages\\u002Findex", contextProps: (function(a){return {apolloIntialState:{ROOT_QUERY:'
'<script>window.__vite_plugin_ssr = {pageId: "\\u002Fpages\\u002Findex", pageContext: (function(a){return {apolloIntialState:{ROOT_QUERY:'
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import App from "../App";
hydrate();

async function hydrate() {
const { Page, contextProps } = await getPage();
const apolloClient = makeApolloClient(contextProps.apolloIntialState);
const { Page, pageContext } = await getPage();
const apolloClient = makeApolloClient(pageContext.apolloIntialState);
ReactDOM.hydrate(
<App apolloClient={apolloClient}>
<Page />
Expand Down
10 changes: 5 additions & 5 deletions examples/graphql-apollo/pages/_default/_default.page.server.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { getDataFromTree } from "@apollo/client/react/ssr";
import App from "../App";

export { render };
export { addContextProps };
export { addPageContext };
export { passToClient };

const passToClient = ["apolloIntialState"];

function render({ contextProps }) {
const { pageHtml } = contextProps;
function render({ pageContext }) {
const { pageHtml } = pageContext;
return html`<!DOCTYPE html>
<html>
<body>
Expand All @@ -19,8 +19,8 @@ function render({ contextProps }) {
</html>`;
}

async function addContextProps({ Page, contextProps }) {
const { apolloClient } = contextProps;
async function addPageContext({ Page, pageContext }) {
const { apolloClient } = pageContext;
const tree = (
<App apolloClient={apolloClient}>
<Page />
Expand Down
4 changes: 2 additions & 2 deletions examples/graphql-apollo/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ async function startServer() {
// Otherwise, our response to a request might include sensitive cached query results
// from a previous request. Source: https://www.apollographql.com/docs/react/performance/server-side-rendering/#example
const apolloClient = makeApolloClient();
const contextProps = { apolloClient };
const result = await renderPage({ url, contextProps });
const pageContext = { apolloClient };
const result = await renderPage({ url, pageContext });
if (result.nothingRendered) return next();
res.status(result.statusCode).send(result.renderResult);
});
Expand Down
Loading

0 comments on commit aedf9fc

Please sign in to comment.