0

我一直在使用vue-meta npm 包来更新我的元标记。但是我看到一个问题,即我放入的值metaInfo()在谷歌搜索描述中没有正确呈现。

metaInfo () {
    return {
      title: 'FAQ',
      meta: [
        {name: 'description', content: 'How can we help you?'},
        {property: 'og:title', content: 'How can we help you?'},
        {property: 'og:site_name', content: 'FAQ site'},
        {property: 'og:description', content: 'How can we help you?'}
      ]
    }
  }

如果我们在浏览器中打开 devtools,我们可以看到在 DOM 中附加了元标记。所以我们现在有两个描述。

  1. 我们放入index.html文件的元描述。
  2. 在 vue-meta 包的帮助下附加在 DOM 中的元描述。

我读了很多关于 vue-meta 的文章,我很难找到不使用 SSR 或 Nuxt 的解决方案,这样我们就可以vue-meta在不使用 SSR 的情况下使用。

4

2 回答 2

1

Unfortunately, this kind of question needs to be asked at the beginning of a project (SSR or not) and usually leads to decide what to use.

Good thing is that Nuxt is not that different from Vue. Since it's just a wrapper on top of Vue, you can throw 90% of your current app in it and it should work pretty fine (had somebody do just that few months ago and it went super smoothly + was pretty fast). Nuxt don't have a lot of drawbacks and essentially add nice things on top of Vue.

You could also give a try to this: https://prerender.io/vue-js/ (it does support SPAs)
But IMO, you'll get more control and flexibility with Nuxt with SSR (either in static or server target).

于 2021-05-20T07:33:12.037 回答
0

是的,这是正确的,如果你想要元标记,你应该使用 nuxtjs。我面临着与网站已经建成相同的问题,但客户要求他也需要元标记。因此,我没有将整个站点迁移到 nuxtjs,而是决定使用 index.php 而不是 index.html。现在我可以根据页面类型添加元标记,而无需将整个项目迁移到 nuxtjs。

假设您在构建项目页面后的 index.html 看起来像

<!DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width,initial-scale=1" name="viewport">
    <link href="/favicon.ico" rel="icon">
    <link href="/assets/fonts/fontawesome-webfont.woff2" rel="stylesheet">
    <link href="/assets/fonts/Edustdy.whtt" rel="stylesheet">
    <link href="/assets/fonts/fontawesome-webfont.woff" rel="stylesheet">
    <link href="/assets/fonts/fontawesome-webfont3e6e.eot" rel="stylesheet">
    <title>default title</title>

    <link href="/css/course.1d10f9e6.css" rel="prefetch">
    <link href="/js/about.b5c0f3ce.js" rel="prefetch">
    <link href="/js/course.d8d75b7d.js" rel="prefetch">
    <link href="/css/app.203f8281.css" rel="preload" as="style">
    <link href="/css/chunk-vendors.e8860705.css" rel="preload" as="style">
    <link href="/js/app.c39400af.js" rel="preload" as="script">
    <link href="/js/chunk-vendors.4cc0d445.js" rel="preload" as="script">
    <link href="/css/chunk-vendors.e8860705.css" rel="stylesheet">
    <link href="/css/app.203f8281.css" rel="stylesheet">
</head>

<body><noscript><strong>We're sorry but this site doesn't work properly without JavaScript enabled. Please enable it to
            continue.</strong></noscript>
    <div id="app"></div>
    <script src="/js/chunk-vendors.4cc0d445.js"></script>
    <script src="/js/app.c39400af.js"></script>

</body>

</html>

我的要求是仅从 API 获取特定页面的元数据,因此 index.php 看起来像

<!DOCTYPE html>
<html lang="">

<head>
<?php

 $current_url = $_SERVER['REQUEST_URI'];
$url = explode('/',$current_url);
if($url[1] == 'course'){
    $response =  shell_exec("curl yourapi.com/course/find?slug=$url[2]");
    $decoded = json_decode($response ,true);
} 
?>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="icon" href="/favicon.ico">
    <?php 
    if(!$decoded['courseName']){
        $decoded['courseName'] = "default title";
    }
    ?>
    <meta name="title" content='<?php echo $decoded['courseName'] ?>'>
      <meta name="description" content='<?php echo $decoded['snippet']?>'>
    <meta property="og:type" content="website">
      <meta property="og:url" content="http://yoursite.com/<?php echo $current_url?>">
      <meta property="og:title" content="<?php echo $decoded['courseName']?>">
      <meta property="og:description" content="<?php echo $decoded['snippet']?>">
      <meta property="og:image" content="<?php echo $decoded['thumbnail'] ?>">
      <meta property="og:image:width" content="800" />
<meta property="og:image:height" content="400" />
    <meta itemprop="name">
    <meta property="og:title">
    <meta name="twitter:title">
    <meta name="description">
    <meta itemprop="description">
    <meta property="og:description">
    <meta name="twitter:description">
    <link rel="stylesheet" href="/assets/fonts/fontawesome-webfont.woff2">
    <link rel="stylesheet" href="/assets/fonts/Edustdy.whtt">
    <link rel="stylesheet" href="/assets/fonts/fontawesome-webfont.woff">
    <link rel="stylesheet" href="/assets/fonts/fontawesome-webfont3e6e.eot">

    <title>default title</title>

    <link href="/css/course.1d10f9e6.css" rel="prefetch">
    <link href="/js/about.b5c0f3ce.js" rel="prefetch">
    <link href="/js/course.d8d75b7d.js" rel="prefetch">
    <link href="/css/app.203f8281.css" rel="preload" as="style">
    <link href="/css/chunk-vendors.e8860705.css" rel="preload" as="style">
    <link href="/js/app.c39400af.js" rel="preload" as="script">
    <link href="/js/chunk-vendors.4cc0d445.js" rel="preload" as="script">
    <link href="/css/chunk-vendors.e8860705.css" rel="stylesheet">
    <link href="/css/app.203f8281.css" rel="stylesheet">
</head>

<body><noscript><strong>We're sorry but this site doesn't work properly without JavaScript enabled. Please enable it to
            continue.</strong></noscript>
    <div id="app"></div>
    <script src="/js/chunk-vendors.4cc0d445.js"></script>
    <script src="/js/app.c39400af.js"></script>

</body>

</html>

现在元标记将在 google、Facebook、Whatsapp 等上可用,但是当您使用 PHP 时,您可能需要考虑一些安全问题,因为我只是通过发送 30 分钟的 PHP 来做到这一点。

于 2021-07-07T13:52:15.170 回答