是的,这是正确的,如果你想要元标记,你应该使用 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 来做到这一点。