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

set base path for open graph requests #173

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ interface CustomPageProps {
pageTitle?: string;
pageDescription?: string;
giftPath?: string;
baseRequestUrl?: string;
}

const defaultPageDescription = 'The easiest way to send and receive cash.';
const defaultPageTitle = 'Boardwalk Cash';

type CustomAppProps = AppProps<CustomPageProps>;
export default function App({ Component, pageProps }: CustomAppProps) {
const { pageTitle, pageDescription, giftPath } = pageProps;
const { pageTitle, pageDescription, giftPath, baseRequestUrl } = pageProps;
useViewportHeight();
const router = useRouter();

const previewImg = `${typeof window !== 'undefined' ? window.location.origin : 'https://boardwalkcash.com'}${giftPath || '/logo-url-preview.png'}`;
const previewImg = `${baseRequestUrl || 'https://boardwalkcash.com'}${giftPath || '/logo-url-preview.png'}`;

useEffect(() => {
const checkAdminAccess = () => {
Expand Down
14 changes: 14 additions & 0 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import { GetServerSideProps, GetServerSidePropsContext } from 'next/types';
import { getRequestedDomainFromRequest } from '@/utils/url';

export default function Home() {
const router = useRouter();
Expand All @@ -16,3 +18,15 @@ export default function Home() {

return;
}

export const getServerSideProps: GetServerSideProps = async (
context: GetServerSidePropsContext,
) => {
const baseRequestUrl = getRequestedDomainFromRequest(context.req);

return {
props: {
baseRequestUrl,
},
};
};
5 changes: 4 additions & 1 deletion src/pages/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useCashuContext } from '@/hooks/contexts/cashuContext';
import { PublicContact, TokenProps, GiftAsset, Currency } from '@/types';
import { findContactByPubkey, isContactsTrustedMint } from '@/lib/contactModels';
import { proofsLockedTo } from '@/utils/cashu';
import { formatUrl } from '@/utils/url';
import { formatUrl, getRequestedDomainFromRequest } from '@/utils/url';
import NotificationDrawer from '@/components/notifications/NotificationDrawer';
import { formatTokenAmount } from '@/utils/formatting';
import { findTokenByTxId } from '@/lib/tokenModels';
Expand Down Expand Up @@ -292,6 +292,8 @@ export const getServerSideProps: GetServerSideProps = async (
let gift: GiftAsset | undefined = undefined;
const txid = context.query.txid as string;

const baseRequestUrl = getRequestedDomainFromRequest(context.req);

if (txid && !token) {
const tokenEntry = await findTokenByTxId(txid);
if (tokenEntry) {
Expand Down Expand Up @@ -338,6 +340,7 @@ export const getServerSideProps: GetServerSideProps = async (
pageTitle: pageTitle(tokenData) || null,
pageDescription: pageDescription(tokenData) || null,
giftPath,
baseRequestUrl,
},
};
};
Expand Down
10 changes: 10 additions & 0 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IncomingMessage } from 'http';
import { NextApiRequest } from 'next';
export const normalizeUrl = (url: string): string => {
url = url.trim();
Expand All @@ -22,3 +23,12 @@ export const getBaseURLFromRequest = (req: NextApiRequest) => {
const protocol = req.headers.referer?.split('://')[0] || 'https';
return `${protocol}://${host}`;
};

export const getRequestedDomainFromRequest = (req: IncomingMessage) => {
const host = req.headers.host;
let protocol = req.headers.referer?.split('://')[0] || 'https';
if (host?.includes('localhost') || host?.includes('127.0.0.1')) {
protocol = 'http';
}
return `${protocol}://${host}`;
};