-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathwithHeadstartWPConfig.ts
More file actions
648 lines (545 loc) · 19.2 KB
/
Copy pathwithHeadstartWPConfig.ts
File metadata and controls
648 lines (545 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
import { setHeadstartWPConfig } from '@headstartwp/core/utils';
import { Rewrite } from 'next/dist/lib/load-custom-routes';
import { withHeadstartWPConfig } from '../withHeadstartWPConfig';
// Mock fs module
jest.mock('fs', () => ({
existsSync: jest.fn(),
readFileSync: jest.fn(),
}));
// Mock path module
jest.mock('path', () => ({
join: jest.fn((...args) => args.join('/')),
resolve: jest.fn((...args) => args.join('/')),
normalize: jest.fn((path) => path),
}));
// Mock require.resolve for next package.json
jest.mock('module', () => ({
...jest.requireActual('module'),
createRequire: jest.fn(() => ({
resolve: jest.fn((path) => {
if (path === 'next/package.json') {
return '/mock/path/next/package.json';
}
throw new Error('Module not found');
}),
})),
}));
describe('withHeadstartWPConfig - Host Check', () => {
let mockExistsSync: jest.Mock;
let mockReadFileSync: jest.Mock;
beforeEach(() => {
// Reset config before each test
setHeadstartWPConfig({});
// Get the mocked functions
// eslint-disable-next-line global-require
const fs = require('fs');
mockExistsSync = fs.existsSync;
mockReadFileSync = fs.readFileSync;
// Mock file system to return a config file
mockExistsSync.mockImplementation((path: string) => {
if (path.includes('headstartwp.config.js') || path.includes('headless.config.js')) {
return true;
}
return false;
});
// Mock readFileSync to return a valid config
mockReadFileSync.mockReturnValue(JSON.stringify({ version: '14.0.0' }));
});
describe('Single Site Configuration', () => {
it('should add has check when site.host is explicitly defined', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
host: 'example.com',
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(8);
// Check that all rewrites have the has check
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite).toHaveProperty('has');
expect(rewrite.has).toEqual([
{ type: 'header', key: 'host', value: 'example.com' },
]);
});
});
it('should add has check when host is inferred from hostUrl', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
hostUrl: 'https://example.com',
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(8);
// Check that all rewrites have the has check with inferred host
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite).toHaveProperty('has');
expect(rewrite.has).toEqual([
{ type: 'header', key: 'host', value: 'example.com' },
]);
});
});
it('should not add has check when neither host nor hostUrl is defined', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(8);
// Check that no rewrites have the has check
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite).not.toHaveProperty('has');
});
});
it('should handle invalid hostUrl gracefully', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
hostUrl: 'invalid-url',
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(8);
// Check that no rewrites have the has check due to invalid URL
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite).not.toHaveProperty('has');
});
});
it('should prefer explicitly defined host over hostUrl', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
host: 'explicit.example.com',
hostUrl: 'https://inferred.example.com',
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(8);
// Check that all rewrites use the explicitly defined host
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite).toHaveProperty('has');
expect(rewrite.has).toEqual([
{ type: 'header', key: 'host', value: 'explicit.example.com' },
]);
});
});
});
describe('Multisite Configuration', () => {
it('should add different has checks for different sites', async () => {
const headlessConfig = {
sites: [
{
sourceUrl: 'https://wp.site1.com',
host: 'site1.com',
},
{
sourceUrl: 'https://wp.site2.com',
hostUrl: 'https://site2.com',
},
{
sourceUrl: 'https://wp.site3.com',
// No host or hostUrl
},
],
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(24); // 8 rewrites per site
// First 8 rewrites should have site1.com host check
for (let i = 0; i < 8; i++) {
expect(rewrites[i]).toHaveProperty('has');
expect(rewrites[i].has).toEqual([
{ type: 'header', key: 'host', value: 'site1.com' },
]);
}
// Next 8 rewrites should have site2.com host check (inferred from hostUrl)
for (let i = 8; i < 16; i++) {
expect(rewrites[i]).toHaveProperty('has');
expect(rewrites[i].has).toEqual([
{ type: 'header', key: 'host', value: 'site2.com' },
]);
}
// Last 8 rewrites should not have has check (no host defined)
for (let i = 16; i < 24; i++) {
expect(rewrites[i]).not.toHaveProperty('has');
}
});
it('should handle mixed host configurations in multisite', async () => {
const headlessConfig = {
sites: [
{
sourceUrl: 'https://wp.example.com',
host: 'example.com',
hostUrl: 'https://example.com',
},
{
sourceUrl: 'https://wp.test.com',
hostUrl: 'https://test.com',
},
],
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(16); // 8 rewrites per site
// All rewrites should have has checks
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite).toHaveProperty('has');
expect(rewrite.has).toHaveLength(1);
expect(rewrite.has?.[0]).toHaveProperty('type', 'header');
expect(rewrite.has?.[0]).toHaveProperty('key', 'host');
expect(['example.com', 'test.com']).toContain(rewrite.has?.[0].value);
});
});
});
describe('Rewrite Structure', () => {
it('should maintain correct rewrite structure with has check', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
host: 'example.com',
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
// Check specific rewrite patterns
const cacheHealthcheckRewrite = (rewrites as Rewrite[]).find(
(r) => r.source === '/cache-healthcheck',
);
expect(cacheHealthcheckRewrite).toBeDefined();
expect(cacheHealthcheckRewrite).toMatchObject({
source: '/cache-healthcheck',
destination: '/api/cache-healthcheck',
has: [{ type: 'header', key: 'host', value: 'example.com' }],
});
const feedRewrite = (rewrites as Rewrite[]).find((r) => r.source === '/feed');
expect(feedRewrite).toBeDefined();
expect(feedRewrite).toMatchObject({
source: '/feed',
destination: 'https://wp.example.com/feed/?rewrite_urls=1',
has: [{ type: 'header', key: 'host', value: 'example.com' }],
});
});
it('should work with existing rewrites', async () => {
const existingRewrites = [
{
source: '/custom-rewrite',
destination: '/custom-destination',
},
];
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
host: 'example.com',
};
const nextConfig = withHeadstartWPConfig(
{ rewrites: () => Promise.resolve(existingRewrites) },
headlessConfig,
);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(9); // 1 existing + 8 default
// Check that existing rewrites are preserved
expect(rewrites[0]).toEqual(existingRewrites[0]);
// Check that default rewrites have has checks
for (let i = 1; i < 9; i++) {
expect(rewrites[i]).toHaveProperty('has');
}
});
});
describe('Edge Cases', () => {
it('should handle empty sites array', async () => {
const headlessConfig = {
sites: [],
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = await nextConfig.rewrites?.();
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(0);
});
it('should handle hostUrl with port', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
hostUrl: 'https://example.com:3000',
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(8);
// Check that host includes port
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite).toHaveProperty('has');
expect(rewrite.has).toEqual([
{ type: 'header', key: 'host', value: 'example.com:3000' },
]);
});
});
it('should handle hostUrl with subdomain', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
hostUrl: 'https://subdomain.example.com',
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(8);
// Check that subdomain is preserved
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite).toHaveProperty('has');
expect(rewrite.has).toEqual([
{ type: 'header', key: 'host', value: 'subdomain.example.com' },
]);
});
});
});
describe('Locale/i18n Handling', () => {
beforeEach(() => {
// Mock file system to ensure we're using pages router (not app router)
// by returning false for app directory checks
// eslint-disable-next-line global-require
const fs = require('fs');
const mockExistsSync = fs.existsSync;
mockExistsSync.mockImplementation((path: string) => {
if (path.includes('headstartwp.config.js') || path.includes('headless.config.js')) {
return true;
}
// Return false for app directory to ensure pages router
if (path.includes('/app') || path.includes('src/app')) {
return false;
}
return false;
});
});
it('should create locale-aware rewrites when i18n is configured for pages router', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
};
const nextConfig = withHeadstartWPConfig(
{
i18n: {
locales: ['en', 'es', 'fr'],
defaultLocale: 'en',
},
},
headlessConfig,
);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
// Should have 7 paths * 4 variants (default + 3 locales) = 28 rewrites
// Plus 1 sitemap xsl rewrite without locale + 3 with locales = 4
// Total: 28 + 4 = 32
expect(rewrites).toHaveLength(32);
// Check that we have rewrites for each locale
const cacheHealthcheckRewrites = (rewrites as Rewrite[]).filter((r) =>
r.source.includes('cache-healthcheck'),
);
expect(cacheHealthcheckRewrites).toHaveLength(4); // default + 3 locales
// Check default rewrite (no locale)
const defaultRewrite = cacheHealthcheckRewrites.find(
(r) => r.source === '/cache-healthcheck',
);
expect(defaultRewrite).toBeDefined();
expect(defaultRewrite?.destination).toBe('/api/cache-healthcheck');
// Check locale-specific rewrites
const enRewrite = cacheHealthcheckRewrites.find(
(r) => r.source === '/en/cache-healthcheck',
);
expect(enRewrite).toBeDefined();
expect(enRewrite?.destination).toBe('/api/cache-healthcheck');
const esRewrite = cacheHealthcheckRewrites.find(
(r) => r.source === '/es/cache-healthcheck',
);
expect(esRewrite).toBeDefined();
expect(esRewrite?.destination).toBe('/api/cache-healthcheck');
const frRewrite = cacheHealthcheckRewrites.find(
(r) => r.source === '/fr/cache-healthcheck',
);
expect(frRewrite).toBeDefined();
expect(frRewrite?.destination).toBe('/api/cache-healthcheck');
});
it('should not create locale-aware rewrites when i18n is not configured', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
};
const nextConfig = withHeadstartWPConfig({}, headlessConfig);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
expect(rewrites).toHaveLength(8); // Standard 8 rewrites without locales
// Verify no locale prefixes in sources
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite.source).not.toMatch(/^\/(en|es|fr)\//);
});
});
it('should create locale-aware rewrites for multisite with i18n', async () => {
const headlessConfig = {
sites: [
{
sourceUrl: 'https://wp.site1.com',
host: 'site1.com',
},
{
sourceUrl: 'https://wp.site2.com',
host: 'site2.com',
},
],
};
const nextConfig = withHeadstartWPConfig(
{
i18n: {
locales: ['en', 'es'],
defaultLocale: 'en',
},
},
headlessConfig,
);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
// 2 sites * 7 paths * 3 variants (default + 2 locales) = 42
// Plus 2 sites * 3 sitemap xsl rewrites (default + 2 locales) = 6
// Total: 42 + 6 = 48
expect(rewrites).toHaveLength(48);
// Check multisite prefix with locales
const site1CacheRewrites = (rewrites as Rewrite[]).filter(
(r) => r.source.includes('_sites/:site') && r.source.includes('cache-healthcheck'),
);
expect(site1CacheRewrites.length).toBeGreaterThan(0);
// Check that we have rewrites with multisite prefix and locales
const multisiteEnRewrite = (rewrites as Rewrite[]).find(
(r) => r.source === '/_sites/:site/en/cache-healthcheck',
);
expect(multisiteEnRewrite).toBeDefined();
const multisiteEsRewrite = (rewrites as Rewrite[]).find(
(r) => r.source === '/_sites/:site/es/cache-healthcheck',
);
expect(multisiteEsRewrite).toBeDefined();
const multisiteDefaultRewrite = (rewrites as Rewrite[]).find(
(r) => r.source === '/_sites/:site/cache-healthcheck',
);
expect(multisiteDefaultRewrite).toBeDefined();
});
it('should handle sitemap stylesheet rewrites with locales', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
};
const nextConfig = withHeadstartWPConfig(
{
i18n: {
locales: ['en', 'es'],
defaultLocale: 'en',
},
},
headlessConfig,
);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
// Find sitemap xsl rewrites - the pattern uses escaped dots
const sitemapXslRewrites = (rewrites as Rewrite[]).filter((r) =>
r.source.includes('main-sitemap'),
);
// Should have default + 2 locales = 3 rewrites
expect(sitemapXslRewrites).toHaveLength(3);
// Check default - pattern uses \\. for escaped dot
const defaultXsl = sitemapXslRewrites.find((r) =>
r.source.includes(':path(.*main-sitemap'),
);
expect(defaultXsl).toBeDefined();
expect(defaultXsl?.source).toBe('/:path(.*main-sitemap\\.xsl)');
// Check locale-specific
const enXsl = sitemapXslRewrites.find((r) => r.source.startsWith('/en/'));
expect(enXsl).toBeDefined();
expect(enXsl?.source).toBe('/en/:path(.*main-sitemap\\.xsl)');
const esXsl = sitemapXslRewrites.find((r) => r.source.startsWith('/es/'));
expect(esXsl).toBeDefined();
expect(esXsl?.source).toBe('/es/:path(.*main-sitemap\\.xsl)');
});
it('should not apply i18n rewrites when using app router', async () => {
// Mock app router by returning true for app directory
// eslint-disable-next-line global-require
const fs = require('fs');
const mockExistsSync = fs.existsSync;
mockExistsSync.mockImplementation((path: string) => {
if (path.includes('headstartwp.config.js') || path.includes('headless.config.js')) {
return true;
}
// Return true for app directory to simulate app router
if (path.includes('/app') || path.includes('src/app')) {
return true;
}
return false;
});
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
};
const nextConfig = withHeadstartWPConfig(
{
i18n: {
locales: ['en', 'es'],
defaultLocale: 'en',
},
},
headlessConfig,
);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
expect(Array.isArray(rewrites)).toBe(true);
// App router should not use Next.js i18n, so should have standard 8 rewrites
expect(rewrites).toHaveLength(8);
// Verify no locale prefixes (app router uses its own i18n system)
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite.source).not.toMatch(/^\/(en|es)\//);
});
});
it('should handle feed rewrites with locales', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
};
const nextConfig = withHeadstartWPConfig(
{
i18n: {
locales: ['en', 'de'],
defaultLocale: 'en',
},
},
headlessConfig,
);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
// Find feed rewrites
const feedRewrites = (rewrites as Rewrite[]).filter((r) => r.source.includes('feed'));
// Should have default + 2 locales = 3 rewrites
expect(feedRewrites).toHaveLength(3);
// Check all have correct destination
feedRewrites.forEach((rewrite) => {
expect(rewrite.destination).toBe('https://wp.example.com/feed/?rewrite_urls=1');
});
// Check sources
const defaultFeed = feedRewrites.find((r) => r.source === '/feed');
expect(defaultFeed).toBeDefined();
const enFeed = feedRewrites.find((r) => r.source === '/en/feed');
expect(enFeed).toBeDefined();
const deFeed = feedRewrites.find((r) => r.source === '/de/feed');
expect(deFeed).toBeDefined();
});
it('should preserve host checks when i18n is configured', async () => {
const headlessConfig = {
sourceUrl: 'https://wp.example.com',
host: 'example.com',
};
const nextConfig = withHeadstartWPConfig(
{
i18n: {
locales: ['en', 'es'],
defaultLocale: 'en',
},
},
headlessConfig,
);
const rewrites = (await nextConfig.rewrites?.()) ?? [];
// All rewrites should have host check
(rewrites as Rewrite[]).forEach((rewrite) => {
expect(rewrite).toHaveProperty('has');
expect(rewrite.has).toEqual([
{ type: 'header', key: 'host', value: 'example.com' },
]);
});
});
});
});