Skip to content

Commit 5181965

Browse files
committed
fix: better types and ensure works with crawlLinks enabled
1 parent b57d6c9 commit 5181965

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

playground/nuxt.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export default defineNuxtConfig({
55
AlgoliaModule
66
],
77
nitro: {
8+
prerender: {
9+
crawlLinks: true
10+
},
811
routeRules: {
912
'/': {
1013
prerender: true

playground/pages/index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
<h3>DocSearch plugin</h3>
1616
<AlgoliaDocSearch :options="docSearch" />
1717
</div>
18+
<NuxtLink to="/other-page">
19+
Other page
20+
</NuxtLink>
1821
</div>
1922
</template>
2023

playground/pages/other-page.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script setup lang="ts">
2+
import { useSeoMeta } from '@unhead/vue'
3+
4+
useHead({
5+
title: 'Other Page'
6+
})
7+
8+
useSeoMeta({
9+
description: 'Dolore amet culpa culpa dolore cupidatat dolor magna amet elit aute qui proident. Eu ea aliquip ipsum est. Incididunt aliqua in consequat reprehenderit adipisicing id laboris eiusmod voluptate laboris tempor ullamco ipsum. Mollit culpa non dolore aute deserunt eiusmod pariatur enim sunt aute consequat quis. Quis sint enim consectetur velit ex ullamco. Dolor non aliqua elit anim reprehenderit ut sit ullamco nulla ut consectetur. Occaecat sit fugiat id consectetur nostrud aute dolore anim ex irure.'
10+
})
11+
</script>
12+
13+
<template>
14+
<div />
15+
</template>

src/hooks/crawler.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11

2-
import type { Nuxt, NuxtHooks } from '@nuxt/schema'
32
import algoliasearch from 'algoliasearch'
43
import scraper from 'metadata-scraper'
54
import type { SearchClient, SearchIndex } from 'algoliasearch'
65
import type { MetaData } from 'metadata-scraper/lib/types'
6+
import { Nuxt } from '@nuxt/schema'
77
import type { ModuleOptions } from './../module'
88

9-
// TODO: Nuxt 2 only
10-
// export type GeneratePageArg = Parameters<NuxtHooks['generate:page']>[0]
11-
export type GeneratePageArg = any
12-
139
export type CrawlerPage = { href: string } & MetaData
1410

11+
type CrawlerMetaGetter = ((html: string, route: string) => MetaData | Promise<MetaData>)
12+
type CrawlerIncludeFilter = ((route: string) => boolean)
13+
14+
export interface CrawlerOptions {
15+
apiKey: string;
16+
indexName: string;
17+
meta?: CrawlerMetaGetter | (keyof MetaData)[]
18+
include?: CrawlerIncludeFilter | (string | RegExp)[]
19+
}
20+
21+
export interface CrawlerConfig extends CrawlerOptions {
22+
meta: CrawlerMetaGetter
23+
include: CrawlerIncludeFilter
24+
}
25+
1526
/**
1627
* Create a function to specify which routes should be indexed.
1728
*/
18-
function createShouldInclude (options: ModuleOptions) {
29+
function createShouldInclude (options: ModuleOptions): CrawlerIncludeFilter {
1930
const { include } = options.crawler
2031

2132
return typeof include === 'function'
@@ -26,7 +37,7 @@ function createShouldInclude (options: ModuleOptions) {
2637
/**
2738
* Create a function to collect the routes' metadata.
2839
*/
29-
function createMetaGetter (options: ModuleOptions) {
40+
function createMetaGetter (options: ModuleOptions): CrawlerMetaGetter {
3041
const { meta } = options.crawler
3142

3243
if (typeof meta === 'function') {
@@ -61,7 +72,7 @@ function createDefaultMetaGetter () {
6172
/**
6273
* Create the "page:generate" hook callback to collect all the included routes' metadata.
6374
*/
64-
export function createPageGenerateHook (nuxt, options: ModuleOptions, pages: CrawlerPage[]) {
75+
export function createPageGenerateHook (nuxt: Nuxt, options: ModuleOptions, pages: CrawlerPage[]) {
6576
const shouldInclude = createShouldInclude(options)
6677
const getMeta = createMetaGetter(options)
6778

@@ -92,7 +103,7 @@ export function createPageGenerateHook (nuxt, options: ModuleOptions, pages: Cra
92103
/**
93104
* Create the "generate:done" hook callback to index the collected routes' metadata.
94105
*/
95-
export function createGenerateDoneHook (nuxt, options: ModuleOptions, pages: CrawlerPage[]) {
106+
export function createGenerateDoneHook (nuxt: Nuxt, options: ModuleOptions, pages: CrawlerPage[]) {
96107
return async () => {
97108
if (pages.length > 0 && options.crawler) {
98109
const { crawler: { apiKey, indexName }, applicationId } = options
@@ -128,5 +139,5 @@ export interface CrawlerHooks {
128139
}
129140

130141
declare module '@nuxt/schema' {
131-
interface NuxtHooks extends CrawlerHooks {}
142+
interface NuxtHooks extends CrawlerHooks { }
132143
}

src/module.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { resolve } from 'path'
22
import { fileURLToPath } from 'url'
3-
import { defineNuxtModule, addPlugin, addComponentsDir, addServerHandler, addImportsDir, isNuxt2, extendViteConfig } from '@nuxt/kit'
4-
import type { MetaData } from 'metadata-scraper/lib/types'
3+
import { defineNuxtModule, addPlugin, addComponentsDir, addServerHandler, addImportsDir, isNuxt2 } from '@nuxt/kit'
54
import { defu } from 'defu'
6-
import { createPageGenerateHook, createGenerateDoneHook, CrawlerPage, CrawlerHooks } from './hooks'
5+
import { createPageGenerateHook, createGenerateDoneHook, CrawlerPage, CrawlerHooks, CrawlerOptions } from './hooks'
76
import type { DocSearchOptions } from './types'
87

98
enum InstantSearchThemes {
@@ -34,14 +33,7 @@ interface ModuleBaseOptions {
3433
}
3534

3635
export interface ModuleOptions extends ModuleBaseOptions {
37-
crawler?: {
38-
apiKey: string;
39-
indexName: string;
40-
meta:
41-
| ((html: string, route: string) => MetaData|Promise<MetaData>)
42-
| (keyof MetaData)[]
43-
include: ((route: string) => boolean) | (string | RegExp)[]
44-
}
36+
crawler?: CrawlerOptions
4537
};
4638

4739
export interface ModuleHooks extends CrawlerHooks {}

0 commit comments

Comments
 (0)