Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skew protection on prefetch without interfering with proxy #1660

Merged
merged 3 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/commons/next-seo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"dependencies": {
"@fern-api/fdr-sdk": "workspace:*",
"next": "npm:@fern-api/[email protected].0",
"next": "npm:@fern-api/[email protected].1",
"react": "^18.2.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"mdast-util-to-hast": "^13.1.0",
"mdx-bundler": "^10.0.2",
"moment": "^2.30.1",
"next": "npm:@fern-api/[email protected].0",
"next": "npm:@fern-api/[email protected].1",
"next-mdx-remote": "^5.0.0",
"nprogress": "^0.2.0",
"numeral": "^2.0.6",
Expand Down
8 changes: 7 additions & 1 deletion packages/ui/app/src/components/FernLink.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OpenNewWindow } from "iconoir-react";
import { atom, useAtomValue } from "jotai";
import Link from "next/link";
import Link, { LinkProps } from "next/link";
import { ReactElement, forwardRef, useEffect, useState, type ComponentProps } from "react";
import { format, parse, resolve, type UrlObject } from "url";
import { useMemoOne } from "use-memo-one";
Expand Down Expand Up @@ -91,6 +91,12 @@ const FernExternalLink = forwardRef<HTMLAnchorElement, FernExternalLinkProps>(

FernExternalLink.displayName = "FernExternalLink";

const LinkWith404Fallback = forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => {
return <Link ref={ref} {...props} />;
});

LinkWith404Fallback.displayName = "LinkWith404Fallback";

export function toUrlObject(url: string | UrlObject): UrlObject {
if (url == null) {
return {};
Expand Down
10 changes: 7 additions & 3 deletions packages/ui/app/src/hooks/useApiRouteSWR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import { FernDocsApiRoute, useApiRoute } from "./useApiRoute";

interface Options<T> extends SWRConfiguration<T, Error, Fetcher<T>> {
disabled?: boolean;
request?: RequestInit;
request?: RequestInit & { headers?: Record<string, string> };
}

function createFetcher<T>(init?: RequestInit): (url: string) => Promise<T> {
return (url: string): Promise<T> => fetch(withSkewProtection(url), init).then((r) => r.json());
function createFetcher<T>(init?: RequestInit & { headers?: Record<string, string> }): (url: string) => Promise<T> {
return async (url: string): Promise<T> => {
const request = { ...init, headers: withSkewProtection(init?.headers) };
const r = await fetch(url, request);
return await r.json();
};
}

export function useApiRouteSWR<T>(route: FernDocsApiRoute, options?: Options<T>): SWRResponse<T> {
Expand Down
3 changes: 1 addition & 2 deletions packages/ui/app/src/hooks/useInterceptNextDataHref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { parseRelativeUrl } from "next/dist/shared/lib/router/utils/parse-relati
import { removeTrailingSlash } from "next/dist/shared/lib/router/utils/remove-trailing-slash";
import { Router } from "next/router";
import { useEffect } from "react";
import { withSkewProtection } from "../util/withSkewProtection";

/**
* This function is adapted from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/page-loader.ts
Expand All @@ -26,7 +25,7 @@ function createPageLoaderGetDataHref(basePath: string | undefined): PageLoader["

const getHrefForSlug = (path: string) => {
const dataRoute = getAssetPathFromRoute(removeTrailingSlash(addLocale(path, locale)), ".json");
return addPathPrefix(`/_next/data/${buildId}${dataRoute}${withSkewProtection(search)}`, basePath);
return addPathPrefix(`/_next/data/${buildId}${dataRoute}${search}`, basePath);
};

const toRet = getHrefForSlug(
Expand Down
25 changes: 18 additions & 7 deletions packages/ui/app/src/util/withSkewProtection.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// const deploymentId = process.env.NEXT_DEPLOYMENT_ID;
// export function withSkewProtection(url: string): string {
// if (!deploymentId) {
// return url;
// }

// if (url.includes("?")) {
// return `${url}&dpl=${deploymentId}`;
// } else {
// return `${url}?dpl=${deploymentId}`;
// }
// }

const deploymentId = process.env.NEXT_DEPLOYMENT_ID;
export function withSkewProtection(url: string): string {

// appears to be a browser bug where `Headers` object is not settable, so we will use a plain object instead
export function withSkewProtection(h?: Record<string, string>): HeadersInit | undefined {
if (!deploymentId) {
return url;
return h;
}

if (url.includes("?")) {
return `${url}&dpl=${deploymentId}`;
} else {
return `${url}?dpl=${deploymentId}`;
}
return new Headers({ ...h, "X-Deployment-Id": deploymentId });
}
2 changes: 1 addition & 1 deletion packages/ui/docs-bundle/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ const nextConfig = {
function withVercelEnv(config) {
return {
...config,
deploymentId: process.env.VERCEL_DEPLOYMENT_ID, // skew protection
deploymentId: process.env.VERCEL_DEPLOYMENT_ID ?? "dpl_development", // skew protection
productionBrowserSourceMaps: process.env.VERCEL_ENV === "preview",
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/docs-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"form-data": "4.0.0",
"jose": "^5.2.3",
"jsonpath": "^1.1.1",
"next": "npm:@fern-api/[email protected].0",
"next": "npm:@fern-api/[email protected].1",
"node-fetch": "2.7.0",
"postcss-import": "^16.0.1",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/fern-docs-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"dependencies": {
"@fern-api/fdr-sdk": "workspace:*",
"next": "npm:@fern-api/[email protected].0",
"next": "npm:@fern-api/[email protected].1",
"path-to-regexp": "6.3.0",
"url-join": "5.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/fontawesome-cdn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@types/react": "^18.0.20",
"depcheck": "^1.4.3",
"eslint": "^8.56.0",
"next": "npm:@fern-api/[email protected].0",
"next": "npm:@fern-api/[email protected].1",
"organize-imports-cli": "^0.10.0",
"prettier": "^3.3.2",
"stylelint": "^16.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/local-preview-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"cssnano": "^6.0.3",
"jsonpath": "^1.1.1",
"lodash-es": "^4.17.21",
"next": "npm:@fern-api/[email protected].0",
"next": "npm:@fern-api/[email protected].1",
"node-fetch": "2.7.0",
"postcss-import": "^16.0.1",
"react": "^18.2.0",
Expand Down
Loading
Loading