Skip to content

Commit

Permalink
fix: issue where http requests were incorrectly determined as https
Browse files Browse the repository at this point in the history
  • Loading branch information
Inlustra committed Jun 28, 2021
1 parent a1870e0 commit 46211f7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion client/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Favicon } from "../../components/Favicon";
import { createIsomorphLink } from "../../../lib/apollo/links";
import { GetServerSideProps } from "next";
import absoluteUrl from "next-absolute-url";
import { getOrigin } from "../../../lib/get-origin";

interface Props {
initialCategories?: FullCategoryFragment[];
Expand Down Expand Up @@ -206,7 +207,7 @@ export function Home({
}

export const getServerSideProps: GetServerSideProps = async ({ req }) => {
const { origin } = absoluteUrl(req);
const origin = getOrigin(req);
const apolloClient = createApolloClient({
links: [createIsomorphLink(origin)],
});
Expand Down
15 changes: 15 additions & 0 deletions lib/get-origin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { IncomingMessage } from "http";

export const getOrigin = (req?: IncomingMessage) => {
let host = req?.headers ? req.headers.host : window.location.host;

if (
req &&
req.headers["x-forwarded-host"] &&
typeof req.headers["x-forwarded-host"] === "string"
) {
host = req.headers["x-forwarded-host"];
}
const protocol = req?.headers.referer?.split("://")?.[0] ?? "http";
return `${protocol}://${host}`;
};
4 changes: 3 additions & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, { useMemo } from "react";
import { Fonts, GlobalFontStyles } from "../client/styles/fonts";
import { createIsomorphLink } from "../lib/apollo/links";
import absoluteUrl from "next-absolute-url";
import { getOrigin } from "../lib/get-origin";

interface MyAppProps extends AppProps {
origin: string;
Expand All @@ -33,8 +34,9 @@ function MyApp(opts: MyAppProps) {
}

MyApp.getInitialProps = async (appContext: AppContext) => {
const { origin } = absoluteUrl(appContext.ctx.req);
const req = appContext.ctx.req;
const appProps = await App.getInitialProps(appContext);
const origin = getOrigin(req);
return {
...appProps,
origin,
Expand Down

0 comments on commit 46211f7

Please sign in to comment.