Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
daf2b2c8
Commit
daf2b2c8
authored
Sep 16, 2022
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix headers and rewrites
parent
7889985f
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
86 additions
and
58 deletions
+86
-58
headers.js
configs/nextjs/headers.js
+0
-6
redirects.js
configs/nextjs/redirects.js
+11
-0
rewrites.js
configs/nextjs/rewrites.js
+10
-7
getCspPolicy.ts
lib/csp/getCspPolicy.ts
+9
-5
middleware.js
middleware.js
+0
-27
middleware.ts
middleware.ts
+49
-0
next.config.js
next.config.js
+7
-13
No files found.
configs/nextjs/headers.js
View file @
daf2b2c8
const
getCspPolicy
=
require
(
'
../../lib/csp/getCspPolicy
'
);
async
function
headers
()
{
return
[
{
...
...
@@ -14,10 +12,6 @@ async function headers() {
key
:
'
X-Content-Type-Options
'
,
value
:
'
nosniff
'
,
},
{
key
:
'
Content-Security-Policy-Report-Only
'
,
value
:
getCspPolicy
(),
},
],
},
];
...
...
configs/nextjs/redirects.js
0 → 100644
View file @
daf2b2c8
async
function
redirects
()
{
return
[
{
source
:
'
/
'
,
destination
:
'
/poa/core
'
,
permanent
:
false
,
},
];
}
module
.
exports
=
redirects
;
configs/nextjs/rewrites.js
View file @
daf2b2c8
const
parseNetworkConfig
=
require
(
'
../../lib/networks/parseNetworkConfig
'
);
async
function
rewrites
()
{
// there can be networks without subtype
// routing in nextjs allows optional params only at the end of the path
// if there are paths with subtype and subsubtype, we will change the routing
// but so far we think we're ok with this hack
const
networksFromConfig
=
parseNetworkConfig
();
return
networksFromConfig
.
filter
(
n
=>
!
n
.
subType
).
map
(
n
=>
({
source
:
`/
${
n
.
type
}
/:slug*`
,
destination
:
`/
${
n
.
type
}
/mainnet/:slug*`
,
}));
//
// UPDATE: as for now I hardcoded all networks without subtype
// because we cannot do proper dynamic rewrites in middleware using runtime ENVs
// see issue - https://github.com/vercel/next.js/discussions/35231
// it seems like it's solved but it's not actually
return
[
{
source
:
'
/astar/:slug*
'
,
destination
:
'
/astar/mainnet/:slug*
'
},
{
source
:
'
/shiden/:slug*
'
,
destination
:
'
/shiden/mainnet/:slug*
'
},
];
}
module
.
exports
=
rewrites
;
lib/csp/getCspPolicy.
j
s
→
lib/csp/getCspPolicy.
t
s
View file @
daf2b2c8
const
parseNetworkConfig
=
require
(
'
../networks/parseNetworkConfig
'
)
;
import
availableNetworks
from
'
lib/networks/availableNetworks
'
;
const
KEY_WORDS
=
{
BLOB
:
'
blob:
'
,
...
...
@@ -16,11 +16,15 @@ const MAIN_DOMAINS = [ '*.blockscout.com', 'blockscout.com' ];
const
isDev
=
process
.
env
.
NODE_ENV
===
'
development
'
;
function
getNetworksExternalAssets
()
{
const
icons
=
parseNetworkConfig
()
const
icons
=
availableNetworks
.
filter
(({
icon
})
=>
typeof
icon
===
'
string
'
)
.
map
(({
icon
})
=>
new
URL
(
icon
));
.
map
(({
icon
})
=>
new
URL
(
icon
as
string
));
return
icons
;
const
logos
=
availableNetworks
.
filter
(({
logo
})
=>
typeof
logo
===
'
string
'
)
.
map
(({
logo
})
=>
new
URL
(
logo
as
string
));
return
icons
.
concat
(
logos
);
}
function
makePolicyMap
()
{
...
...
@@ -121,4 +125,4 @@ function getCspPolicy() {
return
policyString
;
}
module
.
exports
=
getCspPolicy
;
export
default
getCspPolicy
;
middleware.js
deleted
100644 → 0
View file @
7889985f
const
{
NextResponse
}
=
require
(
'
next/server
'
);
const
{
NAMES
}
=
require
(
'
lib/cookies
'
);
const
{
link
}
=
require
(
'
lib/link/link
'
);
const
findNetwork
=
require
(
'
lib/networks/findNetwork
'
).
default
;
export
function
middleware
(
req
)
{
const
[
,
networkType
,
networkSubtype
]
=
req
.
nextUrl
.
pathname
.
split
(
'
/
'
);
const
networkParams
=
{
network_type
:
networkType
,
network_sub_type
:
networkSubtype
,
};
const
selectedNetwork
=
findNetwork
(
networkParams
);
if
(
selectedNetwork
)
{
const
apiToken
=
req
.
cookies
.
get
(
NAMES
.
API_TOKEN
);
if
(
!
apiToken
)
{
const
authUrl
=
link
(
'
auth
'
,
networkParams
);
return
NextResponse
.
redirect
(
authUrl
);
}
}
}
export
const
config
=
{
matcher
:
'
/:network_type/:network_sub_type/account/:path*
'
,
};
middleware.ts
0 → 100644
View file @
daf2b2c8
import
type
{
NextRequest
}
from
'
next/server
'
;
import
{
NextResponse
}
from
'
next/server
'
;
import
{
NAMES
}
from
'
lib/cookies
'
;
import
getCspPolicy
from
'
lib/csp/getCspPolicy
'
;
import
{
link
}
from
'
lib/link/link
'
;
import
findNetwork
from
'
lib/networks/findNetwork
'
;
const
cspPolicy
=
getCspPolicy
();
export
function
middleware
(
req
:
NextRequest
)
{
const
isPageRequest
=
req
.
headers
.
get
(
'
accept
'
)?.
includes
(
'
text/html
'
);
if
(
!
isPageRequest
)
{
return
;
}
const
[
,
networkType
,
networkSubtype
]
=
req
.
nextUrl
.
pathname
.
split
(
'
/
'
);
const
networkParams
=
{
network_type
:
networkType
,
network_sub_type
:
networkSubtype
,
};
const
selectedNetwork
=
findNetwork
(
networkParams
);
if
(
!
selectedNetwork
)
{
return
;
}
// we don't have any info from router here, so just do straight forward sub-string search (sorry)
const
isAccountRoute
=
req
.
nextUrl
.
pathname
.
includes
(
'
/account/
'
);
const
apiToken
=
req
.
cookies
.
get
(
NAMES
.
API_TOKEN
);
if
(
isAccountRoute
&&
!
apiToken
)
{
const
authUrl
=
link
(
'
auth
'
,
networkParams
);
return
NextResponse
.
redirect
(
authUrl
);
}
const
res
=
NextResponse
.
next
();
res
.
headers
.
append
(
'
Content-Security-Policy-Report-Only
'
,
cspPolicy
);
return
res
;
}
/**
* Configure which routes should pass through the Middleware.
* Exclude all `_next` urls.
*/
export
const
config
=
{
matcher
:
[
'
/
'
,
'
/:notunderscore((?!_next).+)
'
],
};
next.config.js
View file @
daf2b2c8
...
...
@@ -3,6 +3,7 @@ const withReactSvg = require('next-react-svg');
const
path
=
require
(
'
path
'
);
const
headers
=
require
(
'
./configs/nextjs/headers
'
);
const
redirects
=
require
(
'
./configs/nextjs/redirects
'
);
const
rewrites
=
require
(
'
./configs/nextjs/rewrites
'
);
const
moduleExports
=
{
...
...
@@ -18,24 +19,17 @@ const moduleExports = {
return
config
;
},
async
redirects
()
{
return
[
{
source
:
'
/
'
,
destination
:
'
/poa/core
'
,
permanent
:
false
,
},
];
},
headers
,
// NOTE: all config functions should be static and not depend on any environment variables
// since all variables will be passed to the app only at runtime and there is now way to change Next.js config at this time
// if you are stuck and strongly believe what you need some sort of flexibility here please fill free to join the discussion
// https://github.com/blockscout/frontend/discussions/167
rewrites
,
redirects
,
headers
,
output
:
'
standalone
'
,
sentry
:
{
hideSourceMaps
:
true
,
},
publicRuntimeConfig
:
{
NEXT_PUBLIC_SUPPORTED_NETWORKS
:
process
.
env
.
NEXT_PUBLIC_SUPPORTED_NETWORKS
,
},
};
const
sentryWebpackPluginOptions
=
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment