I'm trying to make a static website using Wordpress and Gatsby everything works fine locally but when I try to use it on a staging site I get this error :
Path: /wp-json
The server response was "401 Unauthorized"
Inner exception message: "You are not currently logged in."
ERROR #11321 PLUGIN
So is there a way to give Gatsby access to my Wordpress website using my credentials ?
My gatsby-config.js :
module.exports = {
siteMetadata: {
},
plugins: [
{
resolve: `gatsby-source-wordpress`,
options: {
baseUrl: `web-staging.******.io`,
protocol: `https`,
hostingWPCOM: false,
useACF: false,
},
},
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
{
resolve: `gatsby-plugin-manifest`,
options: {
name: `gatsby-starter-default`,
short_name: `starter`,
start_url: `/`,
background_color: `#663399`,
theme_color: `#663399`,
display: `minimal-ui`,
icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
},
},
],
}
See the options for the plugin, specifically auth:
auth: {
// If auth.user and auth.pass are filled, then the source plugin will be allowed
// to access endpoints that are protected with .htaccess.
htaccess_user: "your-htaccess-username",
htaccess_pass: "your-htaccess-password",
Related
I have this issue were I'm trying to request images I've uploaded on my WordPress instance with graphql from Gatsby but I only get the most recent item I uploaded on wordpress.
When i query using the Graphiql IDE on WordPress I the desired results. See this image ➡️
Image of the query and response in get from Graphiql IDE on wordpress
Below is the query and response in get from Graphiql IDE on wordpress
// Graphiql IDE query
{
mediaItems {
edges {
node {
databaseId
}
}
}
}
// The response
{
"data": {
"mediaItems": {
"edges": [
{
"node": {
"databaseId": 30
}
},
{
"node": {
"databaseId": 28
}
},
{
"node": {
"databaseId": 26
}
},
{
"node": {
"databaseId": 20
}
}
]
}
},
"extensions": {
"debug": []
}
}
but when I query from the http://localhost:8000/___graphql endpoint on my local development as I said earlier I only get the most recent upload.
Image of local development query and response
the code is below
// The query to http://localhost:8000/___graphql endpoint
{
wpMediaItem {
altText
localFile {
childImageSharp {
gatsbyImageData(placeholder: TRACED_SVG, width: 100)
id
}
}
}
}
// The response
{
"data": {
"wpMediaItem": {
"altText": "background lines",
"localFile": {
"childImageSharp": null
}
}
},
"extensions": {}
}
The ednpoints I can query
the enpoints img 1
the enpoints img 2
Below is my gatsby-config.js file
const path = require("path");
// Get paths of Gatsby's required rules, which as of writing is located at:
// https://github.com/gatsbyjs/gatsby/tree/fbfe3f63dec23d279a27b54b4057dd611dce74bb/packages/
// gatsby/src/utils/eslint-rules
const gatsbyRequiredRules = path.join(
process.cwd(),
"node_modules",
"gatsby",
"dist",
"utils",
"eslint-rules"
);
module.exports = {
siteMetadata: {
title: `########`,
description: `################`,
author: `###################`,
siteUrl: `###############`,
},
plugins: [
`gatsby-plugin-image`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
{
resolve: `gatsby-plugin-manifest`,
options: {
name: `gatsby-starter-default`,
short_name: `starter`,
start_url: `/`,
background_color: `#663399`,
// This will impact how browsers show your PWA/website
// https://css-tricks.com/meta-theme-color-and-trickery/
// theme_color: `#663399`,
display: `minimal-ui`,
icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
},
},
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-source-wordpress`,
options: {
url: `http://34.133.115.37/graphql`,
},
},
{
resolve: `gatsby-plugin-google-fonts`,
options: {
fonts: [`Poppins`],
display: `swap`,
},
},
{
resolve: `gatsby-plugin-styled-components`,
options: {
// Add any options here
},
},
{
resolve: "gatsby-plugin-eslint",
options: {
// Gatsby required rules directory
rulePaths: [gatsbyRequiredRules],
// Default settings that may be ommitted or customized
stages: ["develop"],
extensions: ["js", "jsx", "ts", "tsx"],
exclude: ["node_modules", "bower_components", ".cache", "public"],
// Any additional eslint-webpack-plugin options below
// ...
},
},
// this (optional) plugin enables Progressive Web App + Offline functionality
// To learn more, visit: https://gatsby.dev/offline
// `gatsby-plugin-offline`,
],
}
I don't know but probably my query is request is wrong, I have tried every can some help
wpMediaItem is not the same node as wpMediaItems (spot the trailing S). The first one is querying a single node, which by default would be sorted by upload date, while wpMediaItems fetch for all images, an array of images (edges in this case), that's why you can get edges in one query and an isolated node in the other, directly accessing for that node data (altText, etc.).
Take a deeper look at the GraphiQL playground (http://localhost:8000/___graphql) to get the correct node, but it should be there.
I want to redirect all traffic to my pricing page via a subdomain to my homepage. I can't figure out how to add wildcards in the has value.
This syntax does not work:
// next.config.js
module.exports = {
target: 'serverless',
async redirects() {
return [
{
source: '/pricing',
has: [
{
type: 'host',
value: '*.*.*',
},
],
permanent: false,
destination: 'https://example.com/'
}
]
},
}
Any ideas?
Vercel support helped me find the solution. I needed to update my regex in the value.
// next.config.js
module.exports = {
target: 'serverless',
async redirects() {
return [
{
source: '/pricing',
has: [
{
type: 'host',
value: '.*\\..*\\..*'
},
],
permanent: false,
destination: 'https://example.com/'
}
]
},
}
If you want to redirect the subdomain, you would add the redirect on that project. If you want the contents of the subdomain (e.g. pricing.example.com) to show up on a different project at /pricing, you would use a rewrite.
module.exports = {
async rewrites() {
return [
{
source: '/pricing',
destination: 'https://pricing.example.com'
},
]
},
}
I am working on a Nuxt project which is deployed on firebase. I have basic auth configuration set up using nuxt-basic-auth-module.
It works locally, but on firebase does not work which I do not why. Am I doing something wrong? Additional settings required? Please give me advices. Thank you very much.
nuxt.config.js
import colors from 'vuetify/es5/util/colors'
require('dotenv').config()
export default {
mode: 'universal',
head: {
titleTemplate: '%s - ' + process.env.npm_package_name,
title: process.env.npm_package_name || '',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: process.env.npm_package_description || '' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
]
},
loading: { color: '#fff' },
buildModules: [
'#nuxtjs/vuetify',
'#nuxtjs/dotenv'
],
modules: [
'#nuxtjs/axios',
'#nuxtjs/proxy',
'nuxt-basic-auth-module'
],
build: {
extend (config, ctx) {
}
},
server: {
port: 4000, // default: 3000
host: '0.0.0.0' // default: localhost
},
env: {
baseUrl: process.env.BASE_URL
},
basic: {
name: 'basicauthusername',
pass: 'basicauthpasswrd1234'
}
}
As mentioned on documentation, enabled comes with false value by default
basic: {
name: 'basicauthusername',
pass: 'basicauthpasswrd1234'
enabled: true // Add this property
}
I am building an app with GatsbyJS. I am using environment variables in my gatsby-config.js. GatsbyJS app builds fine locally, by using .env.* files. However, when building from AWS Amplify it complains about an invalid value retrieved from environment variables. Indeed, it seems that when using process.env.MY_VAR inside gatsby-config.js the value retrieved is encrypted (as per AWSAmplify docs).
I tried with hardcoding the value of the env. var to confirm that encryption was the problem.
The error that I am getting is:
TypeError [ERR_INVALID_URL]: Invalid URL: 6fbaeed85a68.
Which clearly indicates that the value retrieved from process.env.HOSTNAME is 6fbaeed85a68 and not the actual value that I provided in AWS Amplify web's interface.
Below is my gatsby-js.config:
const path = require(`path`);
const queries = require('./src/utils/algolia');
const feedOptions = require('./src/utils/feed');
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`,
});
module.exports = {
siteMetadata: {
siteUrl: new URL(process.env.HOSTNAME).href,
title: `APP_TITLE`,
},
plugins: [
{
resolve: `gatsby-source-kentico-cloud`,
options: {
deliveryClientConfig: {
projectId: process.env.KENTICO_PROJECT_ID,
},
languageCodenames: process.env.KENTICO_LANGUAGES.split(';'),
},
},
{
resolve: `gatsby-plugin-algolia`,
options: {
appId: process.env.GATSBY_ALGOLIA_APP_ID,
apiKey: process.env.ALGOLIA_ADMIN_KEY,
queries,
chunkSize: 10000,
},
},
`gatsby-plugin-react-helmet`,
`gatsby-plugin-sitemap`,
{
resolve: `gatsby-plugin-manifest`,
options: {
name: `APP_NAME`,
short_name: `APP_SHORT_NAME`,
start_url: `/`,
background_color: `#dbdcd1`,
theme_color: `#1ad2eb`,
display: `standalone`,
icon: `src/images/logo-simple-transp-square-260x260.png`,
include_favicon: true,
},
},
`gatsby-plugin-offline`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: path.join(__dirname, `src`, `images`),
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
{
resolve: `gatsby-plugin-sass`,
options: {
includePaths: ['src/styles/_variables'],
},
},
{
resolve: 'gatsby-plugin-mailchimp',
options: {
endpoint: process.env.MAILCHIMP_ENDPOINT,
},
},
{
resolve: 'gatsby-plugin-transition-link',
options: {
layout: require.resolve(`./src/layout`),
},
},
{
resolve: `gatsby-plugin-feed`,
options: feedOptions,
},
{
resolve: `gatsby-plugin-google-tagmanager`,
options: {
id: process.env.GTM_CODE,
includeInDevelopment: false,
},
},
],
};
I don't understand how I am supposed to retrieve env vars. Any help would be greatly appreciated.
When adding environment variables to AWS Amplify App under App Setting -> Environment Variables, just prefix GATSBY_ to all your environment variable names. Remember to change your code to use the new names.
Adding GATSBY_ makes env variables accessible to browser javascript.
Read more about it in the official documentation.
I am working with the gatsby-source-wordpress plugin
If I hard code my API keys/secret into my Gatsby.config, everything works fine, but I want to add these as .env variables so that I can .gitignore for deployment, and this is where things are breaking.
At the root of my directory, I have a .env file which looks like this
CLIENT_SECRET=10987654321
CLIENT_ID=123456
USER=secret#secret.com
PASS=mypassword1
I'm then try to access these in gatsby.config, like this
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`
});
module.exports = {
siteMetadata: {
title: 'Gatsby Default Starter',
},
plugins: [
{
resolve: 'gatsby-source-wordpress',
options: {
baseUrl: 'myurl.com',
protocol: 'http',
hostingWPCOM: true,
useACF: false,
auth: {
wpcom_app_clientSecret: `${process.env.CLIENT_SECRET}`,
wpcom_app_clientId: `${process.env.CLIENT_ID}`,
wpcom_user: `${process.env.USER}`,
wpcom_pass: `${process.env.PASS}`,
},
},
},
{
resolve: `gatsby-plugin-emotion`,
},
'gatsby-plugin-react-helmet',
{
resolve: `gatsby-plugin-manifest`,
options: {
name: 'gatsby-starter-default',
short_name: 'starter',
start_url: '/',
background_color: '#663399',
theme_color: '#663399',
display: 'minimal-ui',
icon: 'src/images/gatsby-icon.png', // This path is
relative to the root of the site.
},
},
'gatsby-plugin-offline',
],
}
which is returning the following errors when I run either gatsby develop or gatsby build
source and transform nodesThe server response was "400 Bad Request"
source and transform nodesThe server response was "403 Forbidden"
Inner exception message : "User cannot access this private blog."
No routes to fetch. Ending.
So, the issue is the .env variables don't seem to be pulling through properly, but I can't see a reason why they wouldn't be? Is there anything I've missed in setting this up?
Gatsby doesn't know which plugin you mean (see How to use) and your overall syntax is wrong. The plugins is an array for example.
module.exports = {
plugins: [
{
resolve: "gatsby-source-wordpress",
options: {
auth: {
wpcom_app_clientSecret: process.env.CLIENT_SECRET,
wpcom_app_clientId: process.env.CLIENT_ID,
wpcom_user: process.env.USER,
wpcom_pass: process.env.PASS,
}
}
}
]
}
This should work assuming that you also define the other necessary fields mentioned in the README.