NEXT.JS - 错误:导出在以下路径上遇到错误:/blogs/:post

时间:2021-05-18 09:25:52

标签: javascript node.js reactjs next.js

我尝试next export

<块引用>

预呈现页面“/blogs/[post]”时发生错误。阅读更多:https://err.sh/next.js/prerender-error 错误:页面 /blogs/[post] 出错:无法导出带有 getServerSideProps 的页面。在此处查看更多信息:https://err.sh/next.js/gssp-export 在 Object.exportPage [默认] (C:_PROJECT\bimeshow-master-27-02-1400\node_modules\next\dist\export\worker.js:15:254) 信息 - 未定义 (33/33) 错误:导出在以下路径上遇到错误: /博客/[帖子] 在 exportApp (C:_PROJECT\bimeshow-master-27-02-1400\node_modules\next\dist\export\index.js:24:1103) 在 processTicksAndRejections (internal/process/task_queues.js:93:5)

博客/[post].js:

...
export const getServerSideProps = async ({params}) => {
    const postUrl = params.post;
    const res = await axios({
        method: 'post',
        url: `/blog/single2?seo_url=${encodeURI(postUrl)}`
    });

    return { 
        props: { 
            data:res!=undefined? res.data.data : []
        }
    }
  }

routes.js:

module.exports = () => {
    return {
      '/': { page: '/' },
      '/about-us': { page: '/about-us' },
      '/404': { page: '/404' },
      '/login': { page: '/login' },
      '/blogs':{ page : '/blogs'},
      '/blogs/[post]': {page: '/blogs/[post]'},
    }

server.js

const express = require('express');
const next = require('next');
const { parse } = require('url');

const DEV = process.env.ENVIRONMENT !== 'production';
const PORT = 4567;

const app = next({dir: '.', dev: DEV});
const handle = app.getRequestHandler();

const getRoutes = require('./routes');

const routes = getRoutes();
  app.prepare().then(() => {
    const server = express();
    server.get('*', (req, res) => {
      const parsedUrl = parse(req.url, true);
      const { pathname, query } = parsedUrl;
      const route = routes[pathname];
      if (route) {
        return app.render(req, res, route.page, route.query);
      }
      return handle(req, res);
  });

  server.listen(PORT, (err) => {
    if (err) throw err;
    console.log(`> READY FOR LIFOTFF http://localhost:${PORT}`);
  });
});

1 个答案:

答案 0 :(得分:0)

正如错误消息中的链接信息已经指出的那样:getServerSideProps 与下一次导出不兼容。您需要将其更改为 getStaticProps。

https://nextjs.org/docs/messages/gssp-export

这是 getStaticProps 的文档:

https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation