Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin to serve a file in a directory #4144

Closed
kevinhughes27 opened this issue Feb 20, 2018 · 11 comments
Closed

Plugin to serve a file in a directory #4144

kevinhughes27 opened this issue Feb 20, 2018 · 11 comments

Comments

@kevinhughes27
Copy link

Hello! I am working on a plugin for Brave payments verification with Gatsby. I need to serve a file at /.well-known/brave-payments-verification.txt.

I followed the documentation on how to create a plugin and read some existing plugins. I wrote the following gatsby-node.js file based on the sitemap plugin:

const fs = require(`fs`)
const Promise = require(`bluebird`)

exports.onPostBuild = (args, pluginOptions) =>
  new Promise(resolve => {
    const path = './public/.well-known'
    const {domain, token} = pluginOptions

    let fileContents = "This is a Brave Payments publisher verification file.\n\n"
    fileContents += `Domain: ${domain}\n`
    fileContents += `Token: ${token}\n`

    if (!fs.existsSync(path)) {
      fs.mkdirSync(path)
    }

    fs.writeFileSync(`${path}/brave-payments-verification.txt`, fileContents)

    resolve()
  })

This worked and generated the appropriate file. I had to add the --dotfiles flag to gh-pages so this folder didn't get ignored during deploy.

in package.json

  "deploy": "gatsby build && cp CNAME public/CNAME && gh-pages --dotfiles -d public"

Finally I added a gatsby-ssr.js to let Gatsby know about the file:

import React from "react"

exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
  const path = `/.well-known/brave-payments-verification.txt`

  setHeadComponents([
    <link
      key={`gatsby-bat-verification`}
      type=`text/plain`
      href={path}
    />,
  ])
}

Everything works locally using gatsby develop and using gatsby build followed by gatsby serve.

However it does not work on production (fails with the error A page wasn't found for "/.well-known/brave-payments-verification.txt" from the loader). I'm deployed to Github pages with Cloudflare for ssl. I've deployed multiple times and cleared the CDN cache. I would expect gatsby serve to produce the same result as the production environment.

I'm lost on how else to debug this 😿 I'd love to get some help to get this working so I can contribute the plugin!

@m-allanson
Copy link
Contributor

Hey @kevinhughes27 👋 did you see the docs on adding static files?

If I understand what you're trying to do, then you should be able to put your file in a static directory at the root of your project like this /static/.well-known/brave-payments-verification.txt. During the build step, that will get copied over and be available at example.com/.well-known/brave-payments-verification.txt.

@kevinhughes27
Copy link
Author

derp - I tried that first actually * but * that was before I found the docs on gh-pages --dotfiles. Let me give this a go.

@kevinhughes27
Copy link
Author

@m-allanson unfortunately this fails with the same error but still works locally. Any other ideas?

@m-allanson
Copy link
Contributor

m-allanson commented Feb 20, 2018

Hmm... So after running gatsby build, the file /public/.well-known/brave-payments-verification.txt exists on your system? But after publishing that directory, visiting example.com/.well-known/brave-payments-verification.txt in a browser gives you a 404 error?

Does /public/.well-known/brave-payments-verification.txt exist on your gh-pages branch?

@kevinhughes27
Copy link
Author

Yup when I build locally it shows up in /public. I push public directly to the gh-pages branch so the .well-known folder is at the root level.

@kevinhughes27
Copy link
Author

I'm assuming its because of the directory or possibly the fact that its a hidden directory. I can look into it further on my own but I'd appreciate some help on where to start in the Gatsby code base

@pieh
Copy link
Contributor

pieh commented Feb 23, 2018

If .well-known/brave-payments-verification.txt is on your gh-pages then I would check CDN side of things

@kevinhughes27
Copy link
Author

I've cleared the CDN a few times and tried turning it off. I get a 404 from Gatsby loader. What else should I try on the CDN?

@m-allanson
Copy link
Contributor

@kevinhughes27 Are you able to share a link to your site's GitHub repo?

@kevinhughes27
Copy link
Author

Oops sorry I should I have posted back. I ended up verifying my site using a DNS record instead. I don't want to make the repo public and it's pretty easy to reproduce if someone wants to look into it further. We can also close the issue.

@ali4heydari
Copy link

I had the same problem with GitHub pages. In local build and serve with Gatsby everythings works fine and /.well-known/brave-payments-verification.txt was reachable, but when deployed to GitHub pages I get 404 error page. Adding .nojekyll file in static folder fixes the issue. See the commit: ali4heydari/ali4heydari.github.io@1fcae2b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants