Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add sitemap plugin #217

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 75 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,18 +505,18 @@ export default defineConfig(({ mode }) => {
output: {
entryFileNames: 'static/client.js',
chunkFileNames: 'static/assets/[name]-[hash].js',
assetFileNames: 'static/assets/[name].[ext]'
}
assetFileNames: 'static/assets/[name].[ext]',
},
},
emptyOutDir: false
}
emptyOutDir: false,
},
}
} else {
return {
ssr: {
external: ['react', 'react-dom']
external: ['react', 'react-dom'],
},
plugins: [honox(), pages()]
plugins: [honox(), pages()],
}
}
})
Expand Down Expand Up @@ -815,6 +815,75 @@ export default defineConfig({
})
```

### Generate Sitemap

To generate a sitemap, use the `honox/dev/sitemap`.

```ts
// app/routes/sitemap.xml.ts
import { Hono } from 'hono'
import { sitemap } from 'hono/vite/sitemap'
import app from '../server'

const route = new Hono()

route.get('/', async c => {
const { data , status, headers } = await sitemap({
app,
hostname: 'https://example.com',
exclude: ['/hidden'],
priority: {'/': '1.0', '/posts/*': '0.6'},
frequency: {'/': 'daily', '/posts/*': 'weekly'},
})
return c.body(
data,
status,
headers
)
})

export default route
```

For deployment to Cloudflare Pages, you can use the following configuration:

Register the IS_PROD = true environment variable in the Cloudflare Pages settings:

1. Navigate to the Cloudflare Workers & Pages Dashboard.
2. Go to [Settings] -> [Environment Variables] -> [Production]
3. Add `IS_PROD` with the value `true`.

Update your `vite.config.ts`:

```ts
// app/routes/sitemap.xml.ts
import { Hono } from 'hono'
import { sitemap } from 'hono/vite/sitemap'
import app from '../server'

const route = new Hono()

route.get('/', async c => {
const { data , status, headers } = await sitemap({
app,
hostname: import.meta.env.IS_PROD ? 'https://example.com' : import.meta.env.CF_PAGES_URL,
exclude: ['/hidden'],
priority: {'/': '1.0', '/posts/*': '0.6'},
frequency: {'/': 'daily', '/posts/*': 'weekly'},
})
return c.body(
data,
status,
headers
)
})

export default route
```

Note: `CF_PAGES_URL` is an environment variable that Cloudflare Pages automatically sets.
For more information, see [Environment Variables](https://developers.cloudflare.com/pages/configuration/build-configuration/#environment-variables).

## Deployment

Since a HonoX instance is essentially a Hono instance, it can be deployed on any platform that Hono supports.
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
"types": "./dist/vite/client.d.ts",
"import": "./dist/vite/client.js"
},
"./vite/sitemap": {
"types": "./dist/vite/sitemap.d.ts",
"import": "./dist/vite/sitemap.js"
},
"./vite/components": {
"types": "./dist/vite/components/index.d.ts",
"import": "./dist/vite/components/index.js"
Expand Down Expand Up @@ -91,6 +95,9 @@
"vite/client": [
"./dist/vite/client"
],
"vite/sitemap": [
"./dist/vite/sitemap"
],
"vite/components": [
"./dist/vite/components"
]
Expand Down
102 changes: 102 additions & 0 deletions src/vite/sitemap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import type { SitemapOptions } from './sitemap'
import sitemap from './sitemap'
import { Hono } from 'hono'

// モックHonoアプリケーションを作成
const createMockApp = (routes: string[]) => {
const app = new Hono()
routes.forEach((route) => {
app.get(route, () => new Response('OK'))
})
return app
}
describe('sitemap', () => {
it('sitemap generator creates valid XML', async () => {
const app = createMockApp(['/', '/about', '/contact'])
const options: SitemapOptions = {
app,
hostname: 'https://example.com',
}

const result = await sitemap(options)

expect(result.status).toBe(200)
expect(result.headers['Content-Type']).toBe('application/xml')
expect(result.data).toContain('<?xml version="1.0" encoding="UTF-8"?>')
expect(result.data).toContain('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">')
expect(result.data).toContain('<loc>https://example.com/</loc>')
expect(result.data).toContain('<loc>https://example.com/about/</loc>')
expect(result.data).toContain('<loc>https://example.com/contact/</loc>')
})

it('sitemap generator respects exclude option', async () => {
const app = createMockApp(['/', '/about', '/contact', '/admin'])
const options: SitemapOptions = {
app,
hostname: 'https://example.com',
exclude: ['/admin'],
}

const result = await sitemap(options)

expect(result.data).not.toContain('<loc>https://example.com/admin/</loc>')
})

it('sitemap generator uses custom frequency and priority', async () => {
const app = createMockApp(['/', '/about'])
const options: SitemapOptions = {
app,
hostname: 'https://example.com',
frequency: {
'/': 'daily',
},
priority: {
'/': '1.0',
},
}

const result = await sitemap(options)

expect(result.data).toContain('<changefreq>daily</changefreq>')
expect(result.data).toContain('<priority>1.0</priority>')
})

it('sitemap generator throws error for invalid priority', async () => {
const app = createMockApp(['/', '/about'])
const options: SitemapOptions = {
app,
hostname: 'https://example.com',
priority: {
'/': '2.0', // 無効な優先度
},
}

await expect(sitemap(options)).rejects.toThrow('Invalid priority value')
})

it('sitemap generator throws error for invalid frequency', async () => {
const app = createMockApp(['/', '/about'])
const options: SitemapOptions = {
app,
hostname: 'https://example.com',
frequency: {
'/': 'annually' as never, // 無効な頻度
},
}

await expect(sitemap(options)).rejects.toThrow('Invalid frequency value')
})

it('sitemap generator uses default values when not provided', async () => {
const app = createMockApp(['/', '/about'])
const options: SitemapOptions = {
app,
}

const result = await sitemap(options)

expect(result.data).toContain('<loc>http://localhost:5173/</loc>')
expect(result.data).toContain('<changefreq>weekly</changefreq>')
expect(result.data).toContain('<priority>0.5</priority>')
})
})
Loading