A Next JS page with a code like:
const eventPage = (props) => { const { event } = props return ( <div> my event { event.name } </div> ) }; export async function getStaticPaths() { return { paths: [], fallback: true }; } export async function getStaticProps({ params }) { console.log('getStaticProps fired...') const event = await request(`events/${params.id}`); // getStaticProps getServerSideProps return { props: { event: event.data || null, } } } export default eventPage; Returns a page with body like
my event undefined
if the page was requested for the FIRST time not in browser but in terminal.
As this page path is not included in paths and the fallback is true (in getStaticPaths function), the page must be generated and cached on first request.
Everything is executed as supposed to, if I run the page in browser. And also everything works fine if I use getInitialProps or getServerSideProps.
But if I use getStaticProps and get the content of the page let's say with Python:
import requests r = requests.get('http://example.com/event/1') with open('pageContent.txt', 'a') as f: f.write(r.content) According to saved page content, I get wrong page and I don't see "getStaticProps fired..." in npm logs.
I remarked this issue after I got empty title and description meta tags for the pages that hadn't been loaded in browser before. I checked meta tags with websites like: http://tools.buzzstream.com/meta-tag-extractor https://metatags.io/
Any idea how to fix this?
https://stackoverflow.com/questions/67309761/getstaticprops-not-working-if-the-page-is-requested-from-cmd-not-browser April 29, 2021 at 09:07AM
没有评论:
发表评论