代码之家  ›  专栏  ›  技术社区  ›  HoCo_

如何处理重定向,同时我的策略返回一个错误

  •  9
  • HoCo_  · 技术社区  · 7 年前

    我正在尝试获取在浏览器中输入的URL,以便在NextJS自定义服务器中进行重定向。此错误仅在开发模式下发生,在生产模式下不发生,因此是否正常?在devmode上要做一些修改来处理这个问题?

    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路径:

    我也尝试过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.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 回复  |  直到 7 年前
        1
  •  2
  •   аlex    6 年前

    这不是下一个.js问题

    只需添加 decodeURIComponent 在你使用的每一个地方 window.location.pathname


    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}`));
    

    并添加 到另一个文件