Skip to content

Commit

Permalink
feat: add support for checking crawlers/bots and integrate `renderToS…
Browse files Browse the repository at this point in the history
…tringAsync()`

- Implemented detection for crawlers and bots
- Updated rendering process to use `renderToStringAsync()` when accessed by crawlers/bots for improved SEO
  • Loading branch information
phonzammi committed Sep 6, 2024
1 parent 72b20ec commit 711742c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/vike-solid/integration/onRenderHtml.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// https://vike.dev/onRenderHtml
import { generateHydrationScript, renderToStream, renderToString } from "solid-js/web";
import { generateHydrationScript, renderToStream, renderToString, renderToStringAsync } from "solid-js/web";
import { dangerouslySkipEscape, escapeInject, stampPipe } from "vike/server";
import { getHeadSetting } from "./getHeadSetting.js";
import { getPageElement } from "./getPageElement.js";
Expand All @@ -10,6 +10,7 @@ import type { PageContextInternal } from "../types/PageContext.js";
import type { Head } from "../types/Config.js";
import type { JSX } from "solid-js/jsx-runtime";
import { isCallable } from "../utils/isCallable.js";
import isBot from "isbot-fast";

export { onRenderHtml };

Expand All @@ -18,7 +19,7 @@ type TPipe = Parameters<typeof stampPipe>[0];
const onRenderHtml: OnRenderHtmlAsync = async (
pageContext: PageContextServer & PageContextInternal,
): ReturnType<OnRenderHtmlAsync> => {
const pageHtml = getPageHtml(pageContext);
const pageHtml = await getPageHtml(pageContext);

const headHtml = getHeadHtml(pageContext);

Expand All @@ -40,10 +41,18 @@ const onRenderHtml: OnRenderHtmlAsync = async (
</html>`;
};

function getPageHtml(pageContext: PageContextServer & PageContextInternal) {
async function getPageHtml(pageContext: PageContextServer & PageContextInternal) {
let pageHtml: string | ReturnType<typeof dangerouslySkipEscape> | TPipe = "";
const userAgent: string | undefined =
pageContext.headers?.["user-agent"] ||
// TODO/eventually: remove old way of acccessing the User Agent header.
// @ts-ignore
pageContext.userAgent;

if (pageContext.Page) {
if (!pageContext.config.stream) {
if (userAgent && isBot(userAgent)) {
pageHtml = dangerouslySkipEscape(await renderToStringAsync(() => getPageElement(pageContext)));
} else if (!pageContext.config.stream) {
pageHtml = dangerouslySkipEscape(renderToString(() => getPageElement(pageContext)));
} else if (pageContext.config.stream === "web") {
pageHtml = renderToStream(() => getPageElement(pageContext), {
Expand Down
1 change: 1 addition & 0 deletions packages/vike-solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"release:commit": "LANG=en_US release-me commit"
},
"dependencies": {
"isbot-fast": "^1.2.0",
"vite-plugin-solid": "^2.10.2"
},
"peerDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions packages/vike-solid/types/isBot.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "isbot-fast" {
function isBot(userAgent: string): boolean;
export = isBot;
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 711742c

Please sign in to comment.