Skip to content

Commit 96b973b

Browse files
chore(refactor): check page before loading its last modified [skip ci]
1 parent 1c06cc7 commit 96b973b

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

src/index/index-page.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import { contains, indexResource } from '@adobe/helix-shared-indexer';
1414
import { cleanupHeaderValue } from '@adobe/helix-shared-utils';
1515
import { fetchPage } from './fetch-page.js';
1616
import {
17-
getIndexTargets, hasSiteConfig, shouldIndex,
18-
getIndexType, getFetchHeaders, containsPath, isSiteConfig,
17+
getIndexType, getFetchHeaders, isSiteConfig,
1918
INTERNAL_SITEMAP_INDEX,
2019
} from './utils.js';
2120

@@ -91,18 +90,7 @@ function indexPageInIndex(path, config, page, log) {
9190
*/
9291
export async function indexPage(context, info, index, retryParams) {
9392
const { config: { content: { source } }, log } = context;
94-
const { webPath, resourcePath, ext } = info;
95-
96-
if (webPath.startsWith('/.helix/') || !containsPath(index, webPath)) {
97-
return new Response('', { status: 204 });
98-
}
99-
100-
const excludes = getIndexTargets(index);
101-
const includeOther = hasSiteConfig(index);
102-
103-
if (!shouldIndex(includeOther, ext) || excludes.includes(resourcePath)) {
104-
return new Response('', { status: 204 });
105-
}
93+
const { webPath, resourcePath } = info;
10694

10795
const url = info.getLiveUrl();
10896
const headers = await getFetchHeaders(context, info);

src/index/status.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12+
import { Response } from '@adobe/fetch';
1213
import { indexPage } from './index-page.js';
14+
import {
15+
getIndexTargets, hasSiteConfig, shouldIndex, containsPath,
16+
} from './utils.js';
1317

1418
/**
1519
* Returns the index records for a resource.
@@ -20,6 +24,18 @@ import { indexPage } from './index-page.js';
2024
* @returns {Promise<Response>} response
2125
*/
2226
export default async function status(context, info, index) {
27+
const { webPath, resourcePath, ext } = info;
28+
if (webPath.startsWith('/.helix/') || !containsPath(index, webPath)) {
29+
return new Response('', { status: 204 });
30+
}
31+
32+
const excludes = getIndexTargets(index);
33+
const includeOther = hasSiteConfig(index);
34+
35+
if (!shouldIndex(includeOther, ext) || excludes.includes(resourcePath)) {
36+
return new Response('', { status: 204 });
37+
}
38+
2339
const response = await indexPage(context, info, index);
2440
return response;
2541
}

src/index/update.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import { Response } from '@adobe/fetch';
1313
import { getContentBusInfo } from '../contentbus/contentbus.js';
1414
import { indexPage } from './index-page.js';
15-
import { sendToQueue } from './utils.js';
15+
import {
16+
getIndexTargets, hasSiteConfig, shouldIndex,
17+
containsPath, sendToQueue,
18+
} from './utils.js';
1619

1720
/**
1821
* Used for our retry count below.
@@ -80,6 +83,18 @@ async function getRetryParams(context, info) {
8083
* @returns {Promise<Response>} response
8184
*/
8285
export default async function update(context, info, index, properties = {}) {
86+
const { webPath, resourcePath, ext } = info;
87+
if (webPath.startsWith('/.helix/') || !containsPath(index, webPath)) {
88+
return new Response('', { status: 204 });
89+
}
90+
91+
const excludes = getIndexTargets(index);
92+
const includeOther = hasSiteConfig(index);
93+
94+
if (!shouldIndex(includeOther, ext) || excludes.includes(resourcePath)) {
95+
return new Response('', { status: 204 });
96+
}
97+
8398
const retryParams = await getRetryParams(context, info);
8499
const response = await indexPage(context, info, index, retryParams);
85100
if (response.status !== 200) {

test/index/update.test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,6 @@ describe('Index Update Tests', () => {
238238
});
239239

240240
it('ignores resources in `.helix`', async () => {
241-
// TODO: should not be required if it won't be indexed
242-
nock.content()
243-
.head('/live/.helix/config.json')
244-
.reply(200, '', { 'x-amz-meta-x-source-last-modified': 'Thu, 08 Jul 2021 11:04:16 GMT' });
245-
246241
const { request, context } = setupTest('/.helix/config.json');
247242
const response = await main(request, context);
248243

test/live/publish.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,6 @@ describe('Publish Action Tests', () => {
339339
nock.content()
340340
.head('/preview/query-index.json')
341341
.reply(200, '', { 'last-modified': 'Thu, 08 Jul 2021 10:04:16 GMT' })
342-
.head('/live/query-index.json')
343-
.reply(404)
344342
.copyObject('/live/query-index.json')
345343
.reply(200, new xml2js.Builder().buildObject({
346344
CopyObjectResult: {
@@ -396,8 +394,6 @@ describe('Publish Action Tests', () => {
396394
nock.content()
397395
.head('/preview/sitemap-index.json')
398396
.reply(200, '', { 'last-modified': 'Thu, 08 Jul 2021 10:04:16 GMT' })
399-
.head('/live/sitemap-index.json')
400-
.reply(404)
401397
.copyObject('/live/sitemap-index.json')
402398
.reply(200, new xml2js.Builder().buildObject({
403399
CopyObjectResult: {

0 commit comments

Comments
 (0)