简体   繁体   English

在 Gatsbyjs 中显示 PDF 文件

[英]Displaying PDF file in Gatsbyjs

I am pretty new to Gatsbyjs and trying to build my portfolio for professional purpose.我对 Gatsbyjs 很陌生,并试图为专业目的建立我的投资组合。 One issue though: when i try to link my pdf the address bar url adds some extra char.但是有一个问题:当我尝试链接我的 pdf 时,地址栏 url 添加了一些额外的字符。 地址栏

import resume from "../../static/samplepdf.pdf"

<li>
     <label htmlFor="resume">resume: </label>
            <a href={resume} target="_blank" rel="noreferrer" id="email">
              <FaNews className="social-icon"></FaNews>
              resume
            </a>
          </li>

As it has been said by @Quentin , this is the normal behavior when dealing with the static folder in Gatsby, the purpose of the hashed URL is to avoid the browser to cache old versions of the asset.正如@Quentin 所说,这是处理 Gatsby 中的static 文件夹时的正常行为,散列的 URL 的目的是避免浏览器缓存资产的旧版本。

However, you can customize it a little bit the output using this approach:但是,您可以使用以下方法对其进行一点自定义 output:

<a href={`/samplepdf.pdf`} target="_blank" rel="noreferrer" id="email">
  <FaNews className="social-icon"></FaNews>
  resume
</a>

What will construct a URL like localhost:8000/samplepdf.pdf什么会构造一个 URL 像localhost:8000/samplepdf.pdf

Ah, so you're combining two approaches here.啊,所以你在这里结合了两种方法。

  1. Importing the file uses Webpack to track the dependency.导入文件使用 Webpack 来跟踪依赖关系。 In this case, the file-loader will hash the file and copy it to your build output (in public ), then return the path to the file (including the hash) as the import.在这种情况下,文件加载器会将 hash 文件复制到您的构建 output(在public中),然后返回文件的路径(包括哈希)作为导入。 So the resume variable here will be a string like /static/lalala.pdf .所以这里的resume变量将是一个类似/static/lalala.pdf的字符串。

  2. For files that you don't want processed via Webpack, Gatsby supports a static folder that will be copied to the build output without modification.对于您不想通过 Webpack 处理的文件,Gatsby 支持static文件夹,该文件夹将被复制到构建 output 中而无需修改。 In this case, your samplepdf.pdf file would be copied to public/static/samplepdf.pdf for you to reference on your site as /static/samplepdf.pdf .在这种情况下,您的samplepdf.pdf文件将被复制到public/static/samplepdf.pdf供您在您的网站上作为/static/samplepdf.pdf参考。 This file might end up with HTTP cache headers set that are irrespective of its contents, leading to stale cached versions.该文件最终可能会设置与其内容无关的 HTTP 缓存标头,从而导致缓存版本过时。 In your specific scenario I wouldn't be too worried about it, but this is something to be mindful of with resources that are embedded on the page (eg images, scripts, etc).在您的特定情况下,我不会太担心它,但是对于嵌入在页面上的资源(例如图像、脚本等),这是需要注意的。

If you aren't concerned with the specific URL being used (and I wouldn't be, generally), I would go with the first approach.如果您不关心正在使用的特定 URL (通常我不会),我会使用第一种方法 go 。 If you are linking to this file from another place on the web and need a consistent URL, go with the second approach.如果您从 web 上的其他位置链接到此文件,并且需要与第二种方法一致的 URL、go。 Or, if you want to use both approaches, you can likely continue as you are without causing any issues short of some confusion for you when you're working on this code in the future.或者,如果您想同时使用这两种方法,您可能会继续原样,而不会在您将来处理此代码时造成任何问题,除非您感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM