Skip to content

Commit

Permalink
Merge pull request #49 from lidofinance/develop
Browse files Browse the repository at this point in the history
from develop to main
  • Loading branch information
infoster42 authored Sep 13, 2023
2 parents d7398fd + aa9f312 commit 83bbf13
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 149 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ARG DEFAULT_CHAIN="1"
ENV NEXT_TELEMETRY_DISABLED=1 \
BASE_PATH=$BASE_PATH \
SUPPORTED_CHAINS=$SUPPORTED_CHAINS \
DEFAULT_CHAIN=$DEFAULT_CHAIN
DEFAULT_CHAIN=$DEFAULT_CHAIN

WORKDIR /app
RUN apk add --no-cache curl=~8
Expand Down
37 changes: 0 additions & 37 deletions middleware.ts

This file was deleted.

41 changes: 32 additions & 9 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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,11 +102,6 @@ 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: '/(.*)',
Expand All @@ -102,16 +114,27 @@ 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',
},
{
key: 'x-content-type-options',
value: 'nosniff',
},
{ key: 'x-xss-protection', value: '1' },
{ key: 'x-download-options', value: 'noopen' },
],
},
{
// 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 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
2 changes: 1 addition & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ 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';
import { withCsp } from 'utilsApi/withCSP';

// Migrations old theme cookies to new cross domain cookies
migrationThemeCookiesToCrossDomainCookiesClientSide();
Expand Down
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 };
};
50 changes: 50 additions & 0 deletions server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { createServer } from 'http';
import { parse } from 'url';
import next from 'next';

const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = Number(process.env.PORT) || 3000;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
// cannot import from next.config.mjs because this will break env load
const CACHE_CONTROL_HEADER = 'x-cache-control';

// allows us to override cache-control header
const overrideSetHeader = (res) => {
const setHeader = res.setHeader;
let cacheControlOverwritten = false;
res.setHeader = function (header, value) {
if (header.toLowerCase() === CACHE_CONTROL_HEADER) {
cacheControlOverwritten = true;
return setHeader.call(this, 'Cache-Control', value);
}

if (header.toLowerCase() === 'cache-control' && cacheControlOverwritten) {
return this;
}

return setHeader.call(this, header, value);
};
};

// eslint-disable-next-line @typescript-eslint/no-floating-promises
app.prepare().then(() => {
createServer(async (req, res) => {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true);

overrideSetHeader(res);

await handle(req, res, parsedUrl);
})
.once('error', (err) => {
console.error(err);
process.exit(1);
})
.listen(port, () => {
console.debug(`> Ready on http://${hostname}:${port}`);
});
});
Loading

0 comments on commit 83bbf13

Please sign in to comment.