'How to use getServerSideProps for every pages in next.js?
I have set a cookie with nookies which store the values of all the products selected by user. I want to fetch the cookie in server side using getServerSideProps and pass the value as props. I have to display the value of cookie on all pages.
When I tried getServerSideProps in _app.js. It did not worked and it did not even run the code.
Is there any way to do it?
Solution 1:[1]
getServerSideProps
does not work in _app.js
. see docs.
you could use the older getInitialProps
in your custom app component but then the automatic static optimisation is disabled, which is something Next.js bets on heavily.
it might be worth digging into your cookie use case and figure out if you really need to read it on the server side.
Solution 2:[2]
As of now, there isn't a built-in way to do it, so I've resorted to doing the following.
First, I created a file that holds the getServerSideProps
function I want to run on every page:
// lib/serverProps.js
export default async function getServerSideProps(ctx) {
// do something
return {
// data
};
}
Then in every page (yes, every, I can't find a workaround; it might even be helpful if you don't need the code to execute on server pages), do:
import getServerSideProps from "../lib/serverProps";
// other stuff...
export { getServerSideProps };
or
// other stuff...
export { default as getServerSideProps } from "../lib/serverProps";
If you want to add other code to run inside getServerSideProps
for a specific page, you could do something along the lines...
import serverProps from "../lib/serverProps";
// other stuff...
export async function getServerSideProps(ctx) {
// do custom page stuff...
return {
...await serverProps(ctx),
...{
// pretend this is what you put inside
// the return block regularly, e.g.
props: { junk: 347 }
}
};
}
Solution 3:[3]
I think you're talking about Pivot Tables. Just follow Microsoft official tutorial it's easy to use
Solution 4:[4]
Like @Harun24HR pointed out, if you’re using Microsoft 365, the UNIQUE array function comes handy. However, for desktop versions of Microsoft Office, you can use PIVOT TABLE or alternatively use the following:
NOTE: Ensure your data is in table format. Sales is the name of the table.
Column A (Quantity): Contains the given quantities
Column B (Price): The corresponding prices of the given quantities
Column C (Rank): Returns the ranks of the prices. In cell C2, enter the code:
=RANK.EQ([@Price],[Price])
Column D (Remove Duplicates): Removes duplicate and get unique ranks of prices. In cell D2, enter:
=IF(COUNTIF(C$2:C2,C2)=1,C2,"")
Column E (Ordering): Order the unique ranks in either ascending or descending order. In cell E2, enter:
=IFERROR(LARGE([REMOVE DUPLICATES],ROW()-ROW(Sales[[#Headers],[REMOVE DUPLICATES]])), "")
Column F (Unique Price): Obtain the unique prices. In F2, enter:
=IFNA(INDEX(Sales[[Price]:[RANK]], MATCH([@ORDERING], [RANK], 0), 1), "")
Column G (Sum of Qty): In cell G2, enter:
=IF([@[UNIQUE PRICE]]="", "", SUMIF([Price], [@[UNIQUE PRICE]], [Quantity]))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | dmudro |
Solution 2 | |
Solution 3 | Dan Ionescu |
Solution 4 | Jovanny |