You can pass page specific props in getServerSideProps like below
import { GetServerSideProps } from "next";
const PageA = () => {
}
export const getServerSideProps: GetServerSideProps = async (ctx) => {
return {
props: {
forbidden: true
}
}
}
export default PageA;
Then you can control that prop value in _app.js file and take action
const App = ({ Component, pageProps }) => {
if (pageProps.forbidden) {
return <Page403 />;
}
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
)
}
So, think reversely.
UPDATE
Okay, so you want _app.js to be your starting point. Here's a way to do so.
_app.js
const App = ({ Component, pageProps }) => {
if (pageProps.forbidden) {
return <Page403 />;
}
return (
<Provider store={store}>
{pageProps.forbidden ? <Component {...pageProps} /> : <Component {...pageProps} testProp={true} />}
</Provider>
)
}
In this technic, we still need to mark the pages we want that specific prop to be existed. For instance, we want that prop to be existed in pages which are not forbidden. Page A, in this case, should not get that prop.
import { GetServerSideProps } from "next";
const PageA = (props) => {
console.log('PageA props', props);//we should not see testProp here
}
export const getServerSideProps: GetServerSideProps = async (ctx) => {
return {
props: {
forbidden: true
}
}
}
export default PageA;
But Page B should get it.
import { GetServerSideProps } from "next";
const PageB = (props) => {
console.log('PageB props', props);//we should see testProp here
}
export const getServerSideProps: GetServerSideProps = async (ctx) => {
return {
props: {}
}
}
export default PageB;
You can modify the logic according to your needs.