【问题标题】:Not working <title><meta> tag build file in NextjsNextjs 中的 <title><meta> 标记构建文件不起作用
【发布时间】:2021-10-15 09:00:39
【问题描述】:

我创建了简单的动态页面。如果我在localhost 上运行它,tilemeta 标签工作正常。当我构建它时,我在查看页面源代码中看不到titlemeta 标记。

您能检查我的代码&lt;title&gt;&lt;meta&gt; 标记部分吗?

category/[list]/[id].js

import { useRouter } from 'next/router'
import Head from 'next/head'
import { Grid, Image, Segment, Card } from "semantic-ui-react"
import 'semantic-ui-css/semantic.min.css'
import Layout from '../../../components/layout'
import Link from "next/link"

const Post = (props) => {
    const router = useRouter()
    const { Id } = router.query
    console.log(props.category_list)
    return (
        <>
            <Layout>
                <Head>
                    <meta charSet="utf-8" />
                    <title>Welcome to Payments, Deals</title>
                    <meta name="description" content="This is sample content" />
                    <meta name="keywords" content="" />
                    <meta name="google-site-verification" content="rtIRrUNRpgZ_lFtlfS8LJ0-8j_d569BXS9NOGa_nM6Y" />
                </Head>
                <Grid className="store-list">
                    <Grid.Column width={16}>
                        <p>
                            <span>{props.category_title.heading_label}</span>
                        </p>
                    </Grid.Column>
                </Grid>
                <Grid columns={4} className="all-offers storeList">
                    {props.category_list.map((x) => {
                        return (
                            <Grid.Column>
                                <Segment>
                                    <Card>
                                        <Link href={"../../store" + "/" + x.name + "/" + x.store_id} passHref={true}>
                                            <a> <Image
                                                src={x.image}
                                                alt=""
                                                className="collection-img"
                                            ></Image>
                                            </a>
                                        </Link>
                                        <Card.Content>
                                            <Link href={"../../store" + "/" + x.name + "/" + x.store_id} passHref={true}>
                                                <a>
                                                    <Card.Header>{x.name}</Card.Header>
                                                </a>
                                            </Link>
                                            <Card.Description>{x.store_summary}</Card.Description>
                                        </Card.Content>
                                        <Card.Content extra>
                                            <p className="rewards">
                                                <span>
                                                    <img src="/images/rewards.png" alt=""></img>
                                                </span> Cash rewards upto <span>AED {x.cashback}</span>
                                            </p>
                                            <p className="location">
                                                <span>
                                                    <img src="/images/location.png" alt=""></img>
                                                </span>
                                                <span className="store-location" key="index">{x.store_branches}</span>
                                                {/* {x.store_branches.map((locations, index) => (
                                                    <span className="store-location">
                                                    {index === 0 ? (
                                                        <span>{locations.store_location}</span>
                                                    ) : index >= 1 ? (
                                                        <span>
                                                        ,&nbsp;&nbsp;{locations.store_location}
                                                        </span>
                                                    ) : null}
                                                    </span>
                                                ))} */}
                                            </p>
                                        </Card.Content>
                                    </Card>
                                </Segment>
                            </Grid.Column>
                        );
                    })}
                </Grid>
            </Layout>
        </>
    )
}

export async function getStaticPaths() {
    // change your API url to get ALL categories
    const topMenu = await fetch('https://Sampleapp.ae/api/v5/web/categories', {
        method: 'POST',
        body: JSON.stringify({ title: "React POST Request Example" }),
        headers: { "Content-Type": "application/json" },

    })
    const menus = await topMenu.json();
    const s = menus.categories;
    // Get the paths we want to pre-render based on posts, play with params variable you are returning
    const paths = s.map((post) => ({
        params:
        {
            list: `${post.store_name}`,
            id: `${post.id}`,
        }
    }))

    // We'll pre-render only these paths at build time.
    // { fallback: blocking } will server-render pages
    // on-demand if the path doesn't exist.
    return { paths, fallback: false }
}
export async function getStaticProps(context) {
    const id = context.params.id;
    const postBody = {
        category_id: id,
        offer_type: "",
    };
    const offerList = await fetch('https://simple.ae/api/v5/web/list', {
        method: 'POST',
        body: JSON.stringify(postBody),
        headers: { "Content-Type": "application/json" },
    })
    const category = await offerList.json();
    // const bookJson = JSON.stringify(book)
    // const bookJson=offerData.stores;
    const category_list = category.stores;
    const category_title = category;
    return {
        props: {
            category_list,
            category_title
        }
    };
}

export default Post;

【问题讨论】:

标签: javascript reactjs next.js


【解决方案1】:

使用 JavaScript 完成的任何 dom 操作都不会在视图源中可见。

查看源代码只显示 HTML 从 Web 服务器传送到我们的浏览器。

【讨论】:

  • 如何在 nextjs 应用程序中显示页面标题和元标记。
  • @IshanBassi Next.js 预渲染服务器上的每个页面,上面的代码应该将元标记添加到返回给浏览器的 HTML 中。
  • @juliomalves,如何修复我的头标签代码上方的预渲染
猜你喜欢
  • 2014-01-31
  • 1970-01-01
  • 2020-04-01
  • 2017-09-11
  • 2014-07-07
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
相关资源
最近更新 更多