diff --git a/plugin/src/create-schema-customization.ts b/plugin/src/create-schema-customization.ts index 55b725a..dd22508 100644 --- a/plugin/src/create-schema-customization.ts +++ b/plugin/src/create-schema-customization.ts @@ -1,7 +1,7 @@ import type { GatsbyNode } from "gatsby" import type { IPluginOptionsInternal } from "./types" import { gatsbyNodeTypeName, normalizeCollections } from "./utils" -import { isString } from "lodash" +import { isArray, isString } from "lodash" /** * By default Gatsby, infers the data types for each node. This can be sometimes brittle or lead to hard-to-debug errors. @@ -22,6 +22,7 @@ export const createSchemaCustomization: GatsbyNode[`createSchemaCustomization`] const { createTypes } = actions const schemaCustomizations = [] + const normalizedUploadTypes = normalizeCollections(pluginOptions.uploadTypes, pluginOptions.endpoint) if (pluginOptions.imageCdn) { schemaCustomizations.push(` @@ -32,20 +33,28 @@ export const createSchemaCustomization: GatsbyNode[`createSchemaCustomization`] height: Int! } `) - const normalizedUploadTypes = normalizeCollections(pluginOptions.uploadTypes, pluginOptions.endpoint) - normalizedUploadTypes.forEach((uploadType) => { - const type = gatsbyNodeTypeName({ - payloadSlug: uploadType.type, - ...(isString(pluginOptions.prefix) && { prefix: pluginOptions.prefix as string }), - }) - console.log(type) + } + + normalizedUploadTypes.forEach((uploadType) => { + const type = gatsbyNodeTypeName({ + payloadSlug: uploadType.type, + ...(isString(pluginOptions.prefix) && { prefix: pluginOptions.prefix as string }), + }) + if (pluginOptions.imageCdn) { schemaCustomizations.push(` type ${type} implements Node { gatsbyImageCdn: Asset @link } `) - }) - } + } + if (pluginOptions.localFiles === true || (isArray(pluginOptions.localFiles) && pluginOptions.localFiles.includes(uploadType.type))) { + schemaCustomizations.push(` + type ${type} implements Node { + localFile: File @link + } + `) + } + }) /** * You most often will use SDL syntax to define your data types. However, you can also use type builders for more advanced use cases diff --git a/plugin/src/plugin-options-schema.ts b/plugin/src/plugin-options-schema.ts index 9dea8c2..9d79404 100644 --- a/plugin/src/plugin-options-schema.ts +++ b/plugin/src/plugin-options-schema.ts @@ -70,8 +70,11 @@ export const pluginOptionsSchema: GatsbyNode["pluginOptionsSchema"] = ({ Joi }): nodePrefix: Joi.string(), // Optional. Map Payload locales to different strings in the resulting nodes. nodeTransform: Joi.object(), - // Optional. Create local file nodes for upload collections. - localFiles: Joi.boolean(), + // Optional. Create local file nodes for upload collections. Pass upload collection slugs to restrict file node creation by collection. + localFiles: Joi.alternatives( + Joi.boolean(), + Joi.array().items(Joi.string()), + ), // Optional. Create Gatsby Image CDN asset nodes for upload collections. imageCdn: Joi.boolean(), /** A base URL for constructing imageUrls. Required for `localFiles`. */ diff --git a/plugin/src/source-nodes.ts b/plugin/src/source-nodes.ts index 8d783b0..26d83fc 100644 --- a/plugin/src/source-nodes.ts +++ b/plugin/src/source-nodes.ts @@ -7,7 +7,7 @@ import { fetchEntity, fetchEntities } from "./fetch" import { normalizeGlobals, payloadImageUrl } from "./utils" import { gatsbyNodeTypeName, documentRelationships } from "./utils" import { createRemoteFileNode } from "gatsby-source-filesystem" -import { get, isString, pickBy } from "lodash" +import { get, isArray, isString, pickBy } from "lodash" import { normalizeCollections } from "./utils" let isFirstSource = true @@ -171,9 +171,9 @@ export const sourceNodes: GatsbyNode[`sourceNodes`] = async (gatsbyApi, pluginOp for (const result of uploadResults) { for (const upload of result) { - let imageCdnId - if (pluginOptions.localFiles) { - createLocalFileNode(context, upload, relationshipIds) + let localFileId, imageCdnId + if (pluginOptions.localFiles === true || (isArray(pluginOptions.localFiles) && pluginOptions.localFiles.includes(upload.gatsbyNodeType))) { + localFileId = await createLocalFileNode(context, upload, relationshipIds) } if (pluginOptions.imageCdn) { imageCdnId = await createAssetNode(context, upload, relationshipIds) @@ -187,6 +187,7 @@ export const sourceNodes: GatsbyNode[`sourceNodes`] = async (gatsbyApi, pluginOp }), data: { ...upload, + ...(localFileId && { localFile: localFileId }), ...(imageCdnId && { gatsbyImageCdn: imageCdnId }), }, }, @@ -281,6 +282,7 @@ export async function createLocalFileNode( value: relationships, }) } + return fileNode.id } export function createAssetNode(context: SourceNodesArgs, data: any, relationshipIds?: { [key: string]: string }) { diff --git a/site/gatsby-config.ts b/site/gatsby-config.ts index cc3eb89..029bb4f 100644 --- a/site/gatsby-config.ts +++ b/site/gatsby-config.ts @@ -81,7 +81,9 @@ const config: GatsbyConfig = { options: { endpoint: process.env.PAYLOAD_BASE_URL, retries: 3, - localFiles: false, + localFiles: [ + 'marketing-site-images' + ], imageCdn: true, baseUrl: process.env.PAYLOAD_CDN_URL, collectionTypes: [