Next項(xiàng)目路徑添加指定的訪問(wèn)前綴方法詳解
前言
開發(fā)多個(gè)項(xiàng)目的時(shí)候,我們希望能通過(guò)指定的前綴路徑去訪問(wèn)不同的項(xiàng)目。比如,通過(guò)前綴 /projectA/
去訪問(wèn)項(xiàng)目 A
;通過(guò)前綴 /projectB/
去訪問(wèn)項(xiàng)目 B
。我們應(yīng)該怎么設(shè)置呢?
上一篇文章中,我們講解了 SPA
項(xiàng)目中 Angular 項(xiàng)目路徑添加指定的訪問(wèn)前綴,本文我們講講 MPA
項(xiàng)目對(duì)路徑前綴的更改。
這里使用的框架是 Next.js
,版本號(hào)為 11.1.2
更改項(xiàng)目前綴
假設(shè)我們添加的前綴為 /jimmy01/
更改頁(yè)面訪問(wèn)前綴
準(zhǔn)確的來(lái)說(shuō),這一步更改的是項(xiàng)目資源的訪問(wèn)前綴,不僅僅是頁(yè)面的前綴。這一步驟,我們統(tǒng)一設(shè)置一個(gè)變量,然后引用資源。
統(tǒng)一設(shè)置的這個(gè)變量,在 next.config.js
文件中:
function getBasePath() { return '/jimmy01' } module.exports = { reactStrictMode: true, basePath: getBasePath(), // 添加前綴 webpack(webpackConfig) { webpackConfig.output.publicPath = getBasePath() + webpackConfig.output.publicPath; //資源生成前綴 return webpackConfig; }, publicRuntimeConfig: { basePath: getBasePath(), //寫入路徑 }, }
然后,我們?cè)诮M件中引用,比如 Foot.js
公共組件:
import { useRef, useEffect } from 'react'; import getConfig from "next/config"; const { publicRuntimeConfig } = getConfig(); const Foot = () => { const refToComponentFoot = useRef(null); useEffect(() => { async function animate() { if(refToComponentFoot.current) { const ScrollReveal = (await import("scrollreveal")).default; // 動(dòng)態(tài)引入 ScrollReveal().reveal(refToComponentFoot.current, { delay: 200 }); } } animate(); }, []) return ( <footer className="text-sm" ref={ refToComponentFoot }> <div className="bg-gray-300"> <div className="max-w-7xl mx-auto px-4 sm:px-6 py-4 sm:py-6 lg:py-8"> <div className="flex flex-col sm:flex-row justify-between items-center justify-start md:space-x-10"> <div className="flex justify-start items-center lg:w-0 lg:flex-1 text-sm text-gray-500"> <a rel="external nofollow" className="text-sm">粵ICP備***號(hào) © ***公司</a> </div> <div className="flex space-x-10 items-center py-6 sm:py-1"> <a href={`${publicRuntimeConfig.basePath}/legal.pdf`} className="font-medium text-gray-500 hover:text-gray-900">法律聲明 & 使用條款</a> </div> <div className="flex items-center justify-end md:flex-1 lg:w-0"> <a href="https://www.***.com/en/" rel="external nofollow" target="_blank" > <img className="h-6 w-auto" src={`${publicRuntimeConfig.basePath}/footer/footer_medical.svg`} alt="medical" /> </a> </div> </div> </div> </div> </footer> ) } export default Foot
也就是引入變量,然后訪問(wèn),上面代碼的訪問(wèn)資源地址比如:"{${publicRuntimeConfig.basePath}/footer/footer_medical.svg
}"。
部署項(xiàng)目
項(xiàng)目開發(fā)完成之后,執(zhí)行打包命令行 npm run build
生成一份構(gòu)建后的壓縮文件夾 out
,將其更名為 jimmy01
,即 out -> jimmy01
。我們將其上傳服務(wù)器指定的路徑,然后用 nginx
進(jìn)行代理。
這里我們更改 nginx.config
中的 server
字段:
server { listen 80 default_server; root /usr/share/nginx/fe/; // 打包的文件存放的位置 location / { index index.html; if (!-e $request_filename){ rewrite ^(.*)$ /$1.html break; break; } } }
執(zhí)行 nginx -s reload
使得配置生效。通過(guò) http://domain.com/jimmy01/index.html
即可訪問(wèn)。
Thanks for reading.
以上就是Next項(xiàng)目路徑添加指定的訪問(wèn)前綴方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Next 路徑指定訪問(wèn)前綴的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
tracking.js實(shí)現(xiàn)前端人臉識(shí)別功能
這篇文章主要介紹了tracking.js實(shí)現(xiàn)前端人臉識(shí)別功能,本文通過(guò)實(shí)例代碼截圖的形式給大家展示的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04JavaScript的public、private和privileged模式
公共/私有變量和方法通過(guò)一個(gè)簡(jiǎn)單的的例子,來(lái)展示如何使用JavaScript在類里面創(chuàng)建私有變量和方法2009-12-12js實(shí)現(xiàn)TAB切換對(duì)應(yīng)不同顏色的代碼
這篇文章主要介紹了js實(shí)現(xiàn)TAB切換對(duì)應(yīng)不同顏色的代碼,涉及javascript頁(yè)面元素的遍歷及樣式的動(dòng)態(tài)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08document.onreadystatechange事件的用法分析
這里主要介紹document.onreadystatechange事件的使用方法, 一般多用于實(shí)時(shí)監(jiān)控用戶的輸入2009-10-10怎樣在CocosCreator中使用物理引擎關(guān)節(jié)
這篇文章主要介紹了怎樣在CocosCreator中使用物理引擎關(guān)節(jié),對(duì)物理引擎感興趣的同學(xué),著重要看一下2021-04-04