Gatsby: Deny direct access to specific pages

Created on 4 Jun 2018  路  3Comments  路  Source: gatsbyjs/gatsby

This is more a question than a gatsby related issue. Is there a way to restrict direct access to a page (for instance: /app/download) and to make it available only from other paths inside the website? Thanks beforehand!

question or discussion

Most helpful comment

This issue talks about how you can pass data using navigateTo API.

src/pages/index.js

import React from 'react'
import Link, { navigateTo } from 'gatsby-link'

const IndexPage = () => (
  <div>
    <Link to="/secret-page">Regular link</Link>
    <button
      onClick={() =>
        navigateTo({
          pathname: '/secret-page',
          state: {
            showPage: true,
          },
        })
      }
    >
      Super secret link
    </button>
  </div>
)

export default IndexPage

src/pages/secret-page.js

import React from 'react'
import Link from 'gatsby-link'

const SecretPage = ({ location }) => {
  const { showPage } = location.state || false

  return showPage ? (
    <div>You have reached a secret page.</div>
  ) : (
    <div>You can鈥檛 access this secret page.</div>
  )
}

export default SecretPage

All 3 comments

This issue talks about how you can pass data using navigateTo API.

src/pages/index.js

import React from 'react'
import Link, { navigateTo } from 'gatsby-link'

const IndexPage = () => (
  <div>
    <Link to="/secret-page">Regular link</Link>
    <button
      onClick={() =>
        navigateTo({
          pathname: '/secret-page',
          state: {
            showPage: true,
          },
        })
      }
    >
      Super secret link
    </button>
  </div>
)

export default IndexPage

src/pages/secret-page.js

import React from 'react'
import Link from 'gatsby-link'

const SecretPage = ({ location }) => {
  const { showPage } = location.state || false

  return showPage ? (
    <div>You have reached a secret page.</div>
  ) : (
    <div>You can鈥檛 access this secret page.</div>
  )
}

export default SecretPage

Oh thank you so much, this works like a charm! Any hint on how to do this for a static file as well?

Hmm, I have no experience with that. Perhaps it requires something like authentication.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benstr picture benstr  路  3Comments

hobochild picture hobochild  路  3Comments

KyleAMathews picture KyleAMathews  路  3Comments

brandonmp picture brandonmp  路  3Comments

jimfilippou picture jimfilippou  路  3Comments