4

我正在学习如何使用 next.js 并按照指南进行操作。我完成了第一部分,最终让您使用 zeit/now 和 express 后端部署一个非常简单的应用程序。在我的本地开发服务器和本地构建上它工作正常,我可以正常导航到所有页面。当我现在部署它时,我的网站工作正常,除了使用后退按钮或第二次导航到索引页面会导致意外错误。即使返回,其他所有页面都可以正常加载。我无法为我的生活找出原因。

这里是网站的例子:https ://test2-mtxtgaubzh.now.sh/

index.js

import Layout from '../components/MyLayout'
import Link from 'next/link'
import fetch from 'isomorphic-unfetch'

const ShowLink = ({ show }) => (
    <li key={show.id}>
          <Link as={`/p/${show.id}`} href={`/post?id=${show.id}`}>
            <a>{show.name}</a>
          </Link>
             <style jsx>{`
                li {
                    list-style: none;
                    margin: 5px 0;
                }

                a { 
                    text-decoration: none;
                    color: blue;
                    font-family: "Arial";
                }

                a:hover {
                    opacity: 0.6;
                }
             `}</style>
   </li>
)
const Index = (props) => (
  <Layout>
    <h1>Terrace House Shows</h1>
        <ul>
            {props.shows.map(({show}) => (
                <ShowLink key={show.id} show={show}/>
            ))}
        </ul>
     <style jsx>{`
         h1, a {
             font-family: "Arial";
         }

         ul {
             padding: 0;
         }

         li {
             list-style: none;
             margin: 5px 0;
         }

         a {
             text-decoration: none;
             color: blue;
         }

         a:hover {
             opacity: 0.6;
         }

    `}</style>
  </Layout>

)

Index.getInitialProps = async function() {
    const res = await fetch('http://api.tvmaze.com/search/shows?q=\%22Terrance%20House\%22"');
    const data = await res.json()
    return {
      shows: data
    }
 }

export default Index

header.js (where the routing comes from)

import Link from 'next/link'

const linkStyle = {
  marginRight: 15
}

const Header = () => (
    <div>
        <Link href="/">
          <a style={linkStyle}>Home</a>
        </Link>
        <Link href="/about">
          <a style={linkStyle}>About</a>
        </Link>
    </div>
)

export default Header

server.js

const express = require('express')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare()
.then(() => {
  const server = express()

  server.get('/p/:id', (req, res) => {
    const actualPage = '/post'
    const queryParams = { id: req.params.id } 
    app.render(req, res, actualPage, queryParams)
  })

  server.get('*', (req, res) => {
    return handle(req, res)
  })

  server.listen(3000, (err) => {
    if (err) throw err
    console.log('> Ready on http://localhost:3000')
  })
})
.catch((ex) => {
  console.error(ex.stack)
  process.exit(1)
})

package.json

{
  "name": "hello-next",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "next",
    "build": "next build",
    "start": "NODE_ENV=production node server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3",
    "isomorphic-unfetch": "^2.0.0",
    "next": "^4.2.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0"
  }
}
4

0 回答 0