如果用户正在搜索特定城市,我想在页面名称之前的网址中显示城市名称,如果用户没有搜索特定城市,那么我想显示正常的网址。我们如何在 Next Js 中实现这一点。
前任。
http://localhost:3000/Delhi/furniture
http://localhost:3000/furniture
Run Code Online (Sandbox Code Playgroud)
两个 url 应指向同一页面。
我尝试使用下一个/链接组件。
<Link href='/furniture' as={config.city ? config.city + '/furniture': 'furniture'}><a>Furniture</a></Link>
Run Code Online (Sandbox Code Playgroud)
Link 有两个 props href 和 as。使用 as 我们可以显示与实际 url 不同的 url,但如果您直接点击由 'as' 属性更改的 url,它将显示 404 页面未找到。我找不到任何可以实现上述目标的 porp 或功能。
当我从 ssr 应用程序收到响应时,我试图将项目设置为会话存储,但它显示错误,会话存储未定义,因为在服务器端没有这样的存储,只有 cookie,但我没有需要那个。
所以我的问题是如何从 NextJS 中的 getInitialProps 将项目设置为sessionStorage
javascript local-storage reactjs server-side-rendering next.js
我正在使用 Angular Universal 10,我想预渲染我的网站。
我对生产环境的正确构建过程感到困惑。
Angular 给出了这两个命令:
"build:ssr": "ng build --prod && ng run my-app.spa:server:production",
"prerender": "ng run my-app.spa:prerender"
Run Code Online (Sandbox Code Playgroud)
仅仅这样做就足以获得生产就绪的输出吗?
npm run prerender
Run Code Online (Sandbox Code Playgroud)
或者我需要同时运行这两个?
npm run build:ssr
npm run prerender
Run Code Online (Sandbox Code Playgroud)
有什么不同?
server-side-rendering angular-cli angular-universal angular angular10
我正在使用 Angular 5。我在我的项目中添加了 Universal,参考: https: //angular.io/guide/universal。
我有 5 个组件,其中 2 个组件:Component1和Component2是静态的,没有任何打字稿代码,其他 3 个组件是动态的,它使用打字稿代码。
我还在我的应用程序中添加了预渲染代码。参考: https: //blog.angular-university.io/angular-universal/
我的问题是,我可以对某些页面使用预渲染,对某些页面使用 ssr,因为我必须改进静态页面的组件 1 和组件 2 的初始加载时间。即使我使用预渲染 ts 文件创建静态 html,当我从应用程序访问它时,它看起来好像已经加载了正常的 ssr 路由。那么,我可以在我的应用程序中进行预渲染和 ssr 吗?如果我可以使用,那么我该怎么做或者如何检查它是否适用于预渲染应用程序。
我正在尝试使用这个react-carousel-3d库https://github.com/suhailssulu/react-carousel-3d,但我收到以下错误,因为该库不是为支持SSR而开发的。
`ReferenceError: window is not defined`
at Object.<anonymous> (C:\Deba\Workspace2021\Nextjs\myportfolio\node_modules\3d-react-carousal\dist\index.js:1:255)
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用没有 SSR 的动态导入https://nextjs.org/docs/advanced-features/dynamic-import#with-no-ssr
const {Carousel} = dynamic(
() => import('../node_modules/3d-react-carousal/src/index.js'),
{ ssr: false }
)
Run Code Online (Sandbox Code Playgroud)
我现在收到以下错误:
./node_modules/3d-react-carousal/src/index.js 189:12
Module parse failed: Unexpected token (189:12)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
render() {
return (
<div className="react-3d-carousel" style={{ height: this.state.height }}>
{this.state.slides && this.state.slides.length > 0 &&
<div className="slider-container">
Run Code Online (Sandbox Code Playgroud)
有人可以指出我在这里做错了什么或者关于如何让它工作的任何想法吗?
当您在服务器 API 端执行简单的控制台登录时,当您使用pnpm run dev.
但是,如果您使用 构建它pnpm run build并运行index.mjs,控制台日志将会显示。
代码:
export default defineEventHandler(async (event) => {
console.log('HAHAHAHAHA');
return 'LOGIN!';
});
Run Code Online (Sandbox Code Playgroud)
这是我的npx nuxi info:
------------------------------
- Operating System: `Linux`
- Node Version: `v18.12.1`
- Nuxt Version: `3.0.0`
- Nitro Version: `1.0.0`
- Package Manager: `pnpm@7.22.0`
- Builder: `vite`
- User Config: `app`, `runtimeConfig`, `modules`, `ssr`, `vite`, `typescript`, `css`, `build`
- Runtime Modules: `@app/ui@1.0.0-alpha`, `nuxt-icon@0.1.8`, `@pinia/nuxt@0.4.6`
- Build Modules: `-`
------------------------------
Run Code Online (Sandbox Code Playgroud) 当我构建独立的 nextjs 项目时,我得到文件 server.js 并尝试运行我的 server.js 文件并收到此错误

我正在开发一个允许学生和教师登录的网站。到目前为止,只有学生登录部分生成 JWT,我正在尝试到达受保护的路由/student。您可以在此处查看代码:
https://github.com/shivpreet16/event-junction
我正在发送根据匹配响应标头中的凭据生成的 JWT,如下所示/api/submit.js:
if (auth.checkPass(rowvals[0].stu_pass, data)) {
const token=({
token: jwt.sign(
{
username: rowvals[0].stu_email,
admin: true,
},
KEY
),
});
// console.log(token)
res.setHeader('Content-Type', 'application/json')
res.setHeader('Authorization', token)
res.setHeader("Set-Cookie",'token=${token}; Max-Age=604800; SameSite=Strict');
res.setHeader("Path", '/student' );
// res.status(200).json({ message: "Login successful" });
res.send(JSON.stringify({ message: 'Logged in'}))
} else res.send(JSON.stringify({ message: "Wrong Password" }));
} else {
res.send(err.message);
}
Run Code Online (Sandbox Code Playgroud)
然后,我从客户端的响应标头接收 JWT /components/Login.js,并尝试使用文件setTokenCookie(token)中的函数将其设置在 cookie 中/utils/auth.js。
const token = response.headers.get('Authorization')
if(token){
console.log("Login successful")
setTokenCookie(token)
console.log(getTokenCookie())
router.push(response.headers.get('Path')) …Run Code Online (Sandbox Code Playgroud) 错误信息:
服务器错误类型错误:createContext 仅适用于客户端组件。在文件顶部添加“use client”指令以使用它。了解更多: https: //nextjs.org/docs/messages/context-in-server-component
import Link from "next/link";
import StyledHero from "./Hero.style";
const Hero = () => {
return (
<StyledHero>
<h1>Stowarzyszenie Juz Lepiej</h1>
<h2>Przybywam w celu:</h2>
<Link about="Panel wolontariusza" href={"/volunteer"}>Uzyskania pomocy</Link>
<Link about="Panel podopiecznego" href={"/mentee"}>Niesienia pomocy</Link>
</StyledHero>
)
};
export default Hero;
Run Code Online (Sandbox Code Playgroud)
import styled from "styled-components";
const StyledHero = styled.main`
width: 100%;
height: auto;
padding: 10em;
`
export default StyledHero;
Run Code Online (Sandbox Code Playgroud)
使用样式组件时出现错误。如何在 Next13 中将 StyledComponents 与 SSR 结合使用?我发现的任何解决方案都不起作用。但是当我更改<StyledHero>为<main></main>之后,它就可以正常工作了。
我尝试在 .babelrc 中使用:
{
"presets": [ …Run Code Online (Sandbox Code Playgroud) javascript typescript server-side-rendering styled-components next.js
我们在 next.js 页面中有以下代码:
<Head>
<title>
Some text {title}
</title>
</Head>
Run Code Online (Sandbox Code Playgroud)
一瞬间,我看到了像 <!--- ---> 这样的 html 注释(类似的东西)
我使用静态站点生成(getServerSideProps),并且我的文件有动态路由,例如[id],所以我也使用getStaticPaths。
我该怎么办?