Skip to content

Commit

Permalink
Merge pull request #47 from lidofinance/feature/si-632-add-custom-ser…
Browse files Browse the repository at this point in the history
…ver-with-proper-cache-control-on-staking

Add custom server
  • Loading branch information
infoster42 authored Sep 11, 2023
2 parents 2c0d072 + fd844c5 commit bc7d054
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 217 deletions.
37 changes: 0 additions & 37 deletions middleware.ts

This file was deleted.

50 changes: 37 additions & 13 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import NextBundleAnalyzer from '@next/bundle-analyzer';
import { createSecureHeaders } from 'next-secure-headers';
import createCSP from './scripts/create-csp.mjs';
import buildDynamics from './scripts/build-dynamics.mjs';

buildDynamics();
Expand Down Expand Up @@ -47,6 +49,23 @@ const rateLimitTimeFrame = process.env.RATE_LIMIT_TIME_FRAME || 60; // 1 minute;
const rewardsBackendAPI = process.env.REWARDS_BACKEND;
const defaultChain = process.env.DEFAULT_CHAIN;

// cache control
export const CACHE_CONTROL_HEADER = 'x-cache-control';
export const CACHE_CONTROL_PAGES = [
'/manifest.json',
'/favicon:size*',
'/',
'/wrap',
'/wrap/unwrap',
'/rewards',
'/referral',
'/withdrawals/request',
'/withdrawals/claim',
'/runtime/window-env.js',
];
export const CACHE_CONTROL_VALUE =
'public, max-age=15, s-max-age=30, stale-if-error=604800, stale-while-revalidate=172800';

const withBundleAnalyzer = NextBundleAnalyzer({
enabled: analyzeBundle,
});
Expand Down Expand Up @@ -85,15 +104,19 @@ export default withBundleAnalyzer({
},
async headers() {
return [
{
// required for gnosis save apps
source: '/manifest.json',
headers: [{ key: 'Access-Control-Allow-Origin', value: '*' }],
},
{
// Apply these headers to all routes in your application.
source: '/(.*)',
headers: [
...createSecureHeaders({
contentSecurityPolicy: createCSP(
cspTrustedHosts,
cspReportUri,
cspReportOnly,
),
frameGuard: false,
referrerPolicy: 'same-origin',
}),
{
key: 'X-DNS-Prefetch-Control',
value: 'on',
Expand All @@ -102,16 +125,17 @@ export default withBundleAnalyzer({
key: 'Strict-Transport-Security',
value: 'max-age=63072000; includeSubDomains; preload',
},
{
key: 'X-Content-Type-Options',
value: 'nosniff',
},
{
key: 'Referrer-Policy',
value: 'same-origin',
},
],
},
{
// required for gnosis save apps
source: '/manifest.json',
headers: [{ key: 'Access-Control-Allow-Origin', value: '*' }],
},
...CACHE_CONTROL_PAGES.map((page) => ({
source: page,
headers: [{ key: CACHE_CONTROL_HEADER, value: CACHE_CONTROL_VALUE }],
})),
];
},
serverRuntimeConfig: {
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"private": true,
"type": "module",
"scripts": {
"dev": "next dev",
"dev": "node server.mjs",
"build": "next build",
"build:analyze": "ANALYZE_BUNDLE=true next build",
"start": "NODE_OPTIONS='-r next-logger' next start",
"start": "NODE_OPTIONS='-r next-logger' NODE_ENV=production node scripts/server.mjs",
"lint": "eslint --ext ts,tsx,js,mjs .",
"lint:fix": "yarn lint --fix",
"types": "tsc --noEmit",
Expand Down Expand Up @@ -37,7 +37,6 @@
"@lidofinance/lido-ui": "^3.8.1",
"@lidofinance/lido-ui-blocks": "2.10.2",
"@lidofinance/next-api-wrapper": "^0.28.0",
"@lidofinance/next-cache-files-middleware": "^0.28.0",
"@lidofinance/next-ip-rate-limit": "^0.28.0",
"@lidofinance/next-pages": "^0.28.0",
"@lidofinance/rpc": "^0.28.0",
Expand Down
5 changes: 1 addition & 4 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'nprogress/nprogress.css';

import Providers from 'providers';
import { nprogress, COOKIES_ALLOWED_FULL_KEY } from 'utils';
import { withCsp } from 'utilsApi/withCsp';
import { BackgroundGradient } from 'shared/components/background-gradient/background-gradient';

// Migrations old theme cookies to new cross domain cookies
Expand Down Expand Up @@ -47,6 +46,4 @@ const AppWrapper = (props: AppProps): JSX.Element => {
);
};

export default process.env.NODE_ENV === 'development'
? AppWrapper
: withCsp(AppWrapper);
export default AppWrapper;
5 changes: 0 additions & 5 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FC } from 'react';
import { GetServerSideProps } from 'next';
import Head from 'next/head';
import { Wallet, StakeForm, LidoStats, StakeFaq } from 'features/home';
import { Layout } from 'shared/components';
Expand All @@ -26,7 +25,3 @@ const Home: FC = () => (
);

export default Home;

export const getServerSideProps: GetServerSideProps = async () => {
return { props: {} };
};
4 changes: 0 additions & 4 deletions pages/rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,3 @@ const Rewards: FC = () => {
};

export default Rewards;

export const getServerSideProps = async () => {
return { props: {} };
};
65 changes: 0 additions & 65 deletions pages/withdrawals/[[...mode]].tsx

This file was deleted.

54 changes: 54 additions & 0 deletions pages/withdrawals/[mode].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { FC } from 'react';
import { GetStaticPaths, GetStaticProps } from 'next';
import Head from 'next/head';
import { useWeb3 } from 'reef-knot/web3-react';

import { Layout } from 'shared/components';
import NoSSRWrapper from 'shared/components/no-ssr-wrapper';

import { WithdrawalsTabs } from 'features/withdrawals';
import { WithdrawalsProvider } from 'features/withdrawals/contexts/withdrawals-context';

const Withdrawals: FC<WithdrawalsModePageParams> = ({ mode }) => {
const { account, chainId } = useWeb3();

return (
<Layout
title="Withdrawals"
subtitle="Request stETH/wstETH withdrawal and claim ETH"
>
<Head>
<title>Withdrawals | Lido</title>
</Head>
<WithdrawalsProvider mode={mode}>
<NoSSRWrapper>
{/* In order to simplify side effects of switching wallets we remount the whole widget, resetting all internal state */}
<WithdrawalsTabs key={`${account ?? '_'}${chainId ?? '1'}`} />
</NoSSRWrapper>
</WithdrawalsProvider>
</Layout>
);
};

export default Withdrawals;

type WithdrawalsModePageParams = {
mode: 'request' | 'claim';
};

export const getStaticPaths: GetStaticPaths<
WithdrawalsModePageParams
> = async () => {
return {
paths: [{ params: { mode: 'request' } }, { params: { mode: 'claim' } }],
fallback: false, // return 404 on non match
};
};

export const getStaticProps: GetStaticProps<
WithdrawalsModePageParams,
WithdrawalsModePageParams
> = async ({ params }) => {
if (!params?.mode) return { notFound: true };
return { props: { mode: params.mode } };
};
32 changes: 14 additions & 18 deletions pages/wrap/[[...mode]].tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import { useEffect } from 'react';
import { GetServerSideProps } from 'next';
import { FC } from 'react';
import { GetStaticPaths, GetStaticProps } from 'next';
import Head from 'next/head';
import { useRouter } from 'next/router';
import { Layout } from 'shared/components';
import { useSafeQueryString } from 'shared/hooks/useSafeQueryString';
import { WrapUnwrapTabs } from 'features/wsteth/wrap-unwrap-tabs';

const WrapPage: React.FC<WrapModePageProps> = ({ mode }) => {
const { isReady, query, replace } = useRouter();
const queryString = useSafeQueryString();

// legacy routing support
useEffect(() => {
if (isReady && query.mode === 'unwrap') {
void replace(`/wrap/unwrap${queryString}`);
}
}, [isReady, query.mode, queryString, replace]);

const WrapPage: FC<WrapModePageProps> = ({ mode }) => {
return (
<Layout
title="Wrap & Unwrap"
Expand All @@ -38,16 +26,24 @@ type WrapModePageProps = {
};

type WrapModePageParams = {
mode: string[] | undefined;
mode: ['unwrap'] | undefined;
};

export const getServerSideProps: GetServerSideProps<
export const getStaticPaths: GetStaticPaths<WrapModePageParams> = async () => {
return {
paths: [{ params: { mode: undefined } }, { params: { mode: ['unwrap'] } }],
fallback: false, // return 404 on non match
};
};

// we need [[...]] pattern for / and /unwrap
export const getStaticProps: GetStaticProps<
WrapModePageProps,
WrapModePageParams
> = async ({ params }) => {
const mode = params?.mode;
if (!mode) return { props: { mode: 'wrap' } };
if (mode.length > 1) return { notFound: true };
if (mode[0] === 'unwrap') return { props: { mode: 'unwrap' } };

return { notFound: true };
};
Loading

0 comments on commit bc7d054

Please sign in to comment.