Skip to content

Commit 333da1b

Browse files
authored
fix(templates): seed from url (#9306)
Update website template seeding to use URLs instead of image assets. Images are not available when deployed to vercel.
1 parent 2b955a4 commit 333da1b

File tree

2 files changed

+124
-36
lines changed
  • templates
    • website/src/endpoints/seed
    • with-vercel-website/src/endpoints/seed

2 files changed

+124
-36
lines changed

templates/website/src/endpoints/seed/index.ts

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import type { CollectionSlug, GlobalSlug, Payload, PayloadRequest } from 'payload'
2-
3-
import fs from 'fs'
4-
import path from 'path'
5-
import { fileURLToPath } from 'url'
1+
import type { CollectionSlug, GlobalSlug, Payload, PayloadRequest, File } from 'payload'
62

73
import { contactForm as contactFormData } from './contact-form'
84
import { contact as contactPageData } from './contact-page'
@@ -13,9 +9,6 @@ import { post1 } from './post-1'
139
import { post2 } from './post-2'
1410
import { post3 } from './post-3'
1511

16-
const filename = fileURLToPath(import.meta.url)
17-
const dirname = path.dirname(filename)
18-
1912
const collections: CollectionSlug[] = [
2013
'categories',
2114
'media',
@@ -46,12 +39,6 @@ export const seed = async ({
4639
// the custom `/api/seed` endpoint does not
4740

4841
payload.logger.info(`— Clearing media...`)
49-
50-
const mediaDir = path.resolve(dirname, '../../public/media')
51-
if (fs.existsSync(mediaDir)) {
52-
fs.rmdirSync(mediaDir, { recursive: true })
53-
}
54-
5542
payload.logger.info(`— Clearing collections and globals...`)
5643

5744
// clear the database
@@ -110,28 +97,65 @@ export const seed = async ({
11097
let demoAuthorID: number | string = demoAuthor.id
11198

11299
payload.logger.info(`— Seeding media...`)
100+
const [image1Buffer, image2Buffer, image3Buffer, hero1Buffer] = await Promise.all([
101+
fetchFileByURL(
102+
'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-post1.webp',
103+
),
104+
fetchFileByURL(
105+
'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-post2.webp',
106+
),
107+
fetchFileByURL(
108+
'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-post3.webp',
109+
),
110+
fetchFileByURL(
111+
'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-hero1.webp',
112+
),
113+
])
114+
// Log all but not the buffer
115+
req.payload.logger.info({
116+
image1Buffer: {
117+
name: image1Buffer.name,
118+
mimetype: image1Buffer.mimetype,
119+
size: image1Buffer.size,
120+
},
121+
image2Buffer: {
122+
name: image2Buffer.name,
123+
mimetype: image2Buffer.mimetype,
124+
size: image2Buffer.size,
125+
},
126+
image3Buffer: {
127+
name: image3Buffer.name,
128+
mimetype: image3Buffer.mimetype,
129+
size: image3Buffer.size,
130+
},
131+
hero1Buffer: {
132+
name: hero1Buffer.name,
133+
mimetype: hero1Buffer.mimetype,
134+
size: hero1Buffer.size,
135+
},
136+
})
113137
const image1Doc = await payload.create({
114138
collection: 'media',
115139
data: image1,
116-
filePath: path.resolve(dirname, 'image-post1.webp'),
140+
file: image1Buffer,
117141
req,
118142
})
119143
const image2Doc = await payload.create({
120144
collection: 'media',
121145
data: image2,
122-
filePath: path.resolve(dirname, 'image-post2.webp'),
146+
file: image2Buffer,
123147
req,
124148
})
125149
const image3Doc = await payload.create({
126150
collection: 'media',
127151
data: image2,
128-
filePath: path.resolve(dirname, 'image-post3.webp'),
152+
file: image3Buffer,
129153
req,
130154
})
131155
const imageHomeDoc = await payload.create({
132156
collection: 'media',
133157
data: image2,
134-
filePath: path.resolve(dirname, 'image-hero1.webp'),
158+
file: hero1Buffer,
135159
req,
136160
})
137161

@@ -360,3 +384,23 @@ export const seed = async ({
360384

361385
payload.logger.info('Seeded database successfully!')
362386
}
387+
388+
async function fetchFileByURL(url: string): Promise<File> {
389+
const res = await fetch(url, {
390+
credentials: 'include',
391+
method: 'GET',
392+
})
393+
394+
if (!res.ok) {
395+
throw new Error(`Failed to fetch file from ${url}, status: ${res.status}`)
396+
}
397+
398+
const data = await res.arrayBuffer()
399+
400+
return {
401+
name: url.split('/').pop() || `file-${Date.now()}`,
402+
data: Buffer.from(data),
403+
mimetype: `image/${url.split('.').pop()}`,
404+
size: data.byteLength,
405+
}
406+
}

templates/with-vercel-website/src/endpoints/seed/index.ts

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import type { CollectionSlug, GlobalSlug, Payload, PayloadRequest } from 'payload'
2-
3-
import fs from 'fs'
4-
import path from 'path'
5-
import { fileURLToPath } from 'url'
1+
import type { CollectionSlug, GlobalSlug, Payload, PayloadRequest, File } from 'payload'
62

73
import { contactForm as contactFormData } from './contact-form'
84
import { contact as contactPageData } from './contact-page'
@@ -13,9 +9,6 @@ import { post1 } from './post-1'
139
import { post2 } from './post-2'
1410
import { post3 } from './post-3'
1511

16-
const filename = fileURLToPath(import.meta.url)
17-
const dirname = path.dirname(filename)
18-
1912
const collections: CollectionSlug[] = [
2013
'categories',
2114
'media',
@@ -46,12 +39,6 @@ export const seed = async ({
4639
// the custom `/api/seed` endpoint does not
4740

4841
payload.logger.info(`— Clearing media...`)
49-
50-
const mediaDir = path.resolve(dirname, '../../public/media')
51-
if (fs.existsSync(mediaDir)) {
52-
fs.rmdirSync(mediaDir, { recursive: true })
53-
}
54-
5542
payload.logger.info(`— Clearing collections and globals...`)
5643

5744
// clear the database
@@ -110,28 +97,65 @@ export const seed = async ({
11097
let demoAuthorID: number | string = demoAuthor.id
11198

11299
payload.logger.info(`— Seeding media...`)
100+
const [image1Buffer, image2Buffer, image3Buffer, hero1Buffer] = await Promise.all([
101+
fetchFileByURL(
102+
'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-post1.webp',
103+
),
104+
fetchFileByURL(
105+
'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-post2.webp',
106+
),
107+
fetchFileByURL(
108+
'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-post3.webp',
109+
),
110+
fetchFileByURL(
111+
'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-hero1.webp',
112+
),
113+
])
114+
// Log all but not the buffer
115+
req.payload.logger.info({
116+
image1Buffer: {
117+
name: image1Buffer.name,
118+
mimetype: image1Buffer.mimetype,
119+
size: image1Buffer.size,
120+
},
121+
image2Buffer: {
122+
name: image2Buffer.name,
123+
mimetype: image2Buffer.mimetype,
124+
size: image2Buffer.size,
125+
},
126+
image3Buffer: {
127+
name: image3Buffer.name,
128+
mimetype: image3Buffer.mimetype,
129+
size: image3Buffer.size,
130+
},
131+
hero1Buffer: {
132+
name: hero1Buffer.name,
133+
mimetype: hero1Buffer.mimetype,
134+
size: hero1Buffer.size,
135+
},
136+
})
113137
const image1Doc = await payload.create({
114138
collection: 'media',
115139
data: image1,
116-
filePath: path.resolve(dirname, 'image-post1.webp'),
140+
file: image1Buffer,
117141
req,
118142
})
119143
const image2Doc = await payload.create({
120144
collection: 'media',
121145
data: image2,
122-
filePath: path.resolve(dirname, 'image-post2.webp'),
146+
file: image2Buffer,
123147
req,
124148
})
125149
const image3Doc = await payload.create({
126150
collection: 'media',
127151
data: image2,
128-
filePath: path.resolve(dirname, 'image-post3.webp'),
152+
file: image3Buffer,
129153
req,
130154
})
131155
const imageHomeDoc = await payload.create({
132156
collection: 'media',
133157
data: image2,
134-
filePath: path.resolve(dirname, 'image-hero1.webp'),
158+
file: hero1Buffer,
135159
req,
136160
})
137161

@@ -360,3 +384,23 @@ export const seed = async ({
360384

361385
payload.logger.info('Seeded database successfully!')
362386
}
387+
388+
async function fetchFileByURL(url: string): Promise<File> {
389+
const res = await fetch(url, {
390+
credentials: 'include',
391+
method: 'GET',
392+
})
393+
394+
if (!res.ok) {
395+
throw new Error(`Failed to fetch file from ${url}, status: ${res.status}`)
396+
}
397+
398+
const data = await res.arrayBuffer()
399+
400+
return {
401+
name: url.split('/').pop() || `file-${Date.now()}`,
402+
data: Buffer.from(data),
403+
mimetype: `image/${url.split('.').pop()}`,
404+
size: data.byteLength,
405+
}
406+
}

0 commit comments

Comments
 (0)