NextJS - Как обрабатывать перенаправление, пока моя стратегия возвращает ошибку - PullRequest
0 голосов
/ 09 октября 2018

Я пытаюсь получить URL-адрес, введенный в браузере, для перенаправления на мой пользовательский сервер NextJS.Эта ошибка возникает только в режиме разработки, нет в производственном режиме, так что это нормально?есть какая-то модификация, чтобы сделать это на devmode?

Я пытался использовать объект pathname.К сожалению, когда я впервые ввел URL в адресную строку, мой путь сначала возвращает:

/ _ next / static / chunks / 0.js

Я попытался с req.rawHeaders.Но моя консоль ничего не возвращает до 15-го пробного периода:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js:

путь req.rawHeaders в next.server.js: / pathTargeted // работа!но немного позже ..

Я также пытался с req.headers.referer, но даже первый путь возвращается не тот путь, который я ввел в URL.

В результате я попал в ошибку 404.Так как избежать этого и всегда получать реальный адрес, введенный в браузере?Это как раз и есть моя проблема.

Вот мой фрагмент реагирования:

import React, {Component} from "react"; 
import style from "./BlogHubTemplate.module.css";

import storeWrapper from "../../HOC/storeWrapper/storeWrapper"
import {connect} from 'react-redux';

import Router from 'next/router'


class BlogHubTemplate extends Component { 

    redirectPost = (postCategory, postTitle) => { 
        Router.replace(`/${postCategory}/${postTitle}`) 
    }

здесь мой пользовательский next.server js:

app.prepare().then(() => {
 createServer((req, res) => {
 // Be sure to pass `true` as the second argument to `url.parse`.
 // This tells it to parse the query portion of the URL.
 const parsedUrl = parse(req.url, true)
 const { pathname, query } = parsedUrl; 

 console.log("req.headers in next.server.js : ", req.headers.referer.substr(22))
 console.log("req.rawHeaders path in next.server.js : ", req.rawHeaders[11].substr(22))

Любая подсказка будет отличной, спасибо

1 Ответ

0 голосов
/ 22 ноября 2018

Это не проблема next.js

, просто добавьте decodeURIComponent в каждое место, где вы используете window.location.pathname


28 строк кода https://github.com/Hocoh/redirect_next/blob/master/ui/pages/post.js#L29

вместо:

  var postFullPath = window.location.pathname.substr(1) ;

должно быть:

  var postFullPath = decodeURIComponent(window.location.pathname).substr(1) ;


38 строка кода https://github.com/Hocoh/redirect_next/blob/master/ui/pages/blog.js#L38

вместо:

var pageTargeted = window.location.pathname.substr(11) ; 

должно быть:

var pageTargeted = decodeURIComponent(window.location.pathname).substr(11) ; 

13 кодовая строка

https://github.com/Hocoh/redirect_next/blob/master/ui/components/BlogHubTemplate/utils/Pagination/Pagination.js#L13

вместо

   window.location.pathname = `blog/page/${pageTargeted}`

должно быть:

  Router.push(decodeURIComponent(`blog/page/${pageTargeted}`))


10 строка кода

https://github.com/Hocoh/redirect_next/blob/master/ui/components/BlogHubTemplate/utils/Pagination/PaginationMain/PaginationMain.js#L10

вместо:

window.location.pathname = `blog/page/${pageTargeted}`

должно быть:

  Router.push(decodeURIComponent(`blog/page/${pageTargeted}`))

строка кода 31

https://github.com/Hocoh/redirect_next/blob/master/ui/components/BlogHubTemplate/BlogHubTemplate.js#L31

вместо:

Router.replace(`/${postCategory}/${postTitle}`);

должно быть:

Router.push(decodeURIComponent(`/${postCategory}/${postTitle}`));

и добавьте decodeURIComponent в другие файлы

...