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 removeHtmlExtension option #183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions @kindspells/astro-shield/src/netlify.mts
Original file line number Diff line number Diff line change
Expand Up @@ -459,5 +459,13 @@ export const patchNetlifyHeadersConfig = async (

const mergedConfig = mergeNetlifyHeadersConfig(baseConfig, patchConfig)

if (securityHeadersOptions.removeHtmlExtension) {
for (const entry of mergedConfig.entries) {
if (typeof entry === 'object' && 'path' in entry) {
entry.path = entry.path.replace(/\.html$/, '')
}
}
}

await writeFile(configPath, serializeNetlifyHeadersConfig(mergedConfig))
}
7 changes: 7 additions & 0 deletions @kindspells/astro-shield/src/types.mts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ export type SecurityHeadersOptions = {
* Defaults to `undefined`.
*/
contentSecurityPolicy?: CSPOptions | undefined

/**
* - If set, it will remove the `.html` extension from the URLs.
*
* Defaults to `false`.
*/
removeHtmlExtension?: boolean
}

export type ShieldOptions = {
Expand Down
7 changes: 7 additions & 0 deletions @kindspells/astro-shield/src/vercel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,12 @@ export const patchVercelHeadersConfig = async (

const mergedConfig = mergeVercelConfig(baseConfig, patchConfig)

if (securityHeadersOptions.removeHtmlExtension) {
for (const route of mergedConfig.routes ?? []) {
if (route.src.endsWith('.html'))
route.src = route.src.slice(0, -5)
}
}

await writeFile(configPath, serializeVercelConfig(mergedConfig))
}
6 changes: 6 additions & 0 deletions docs/src/content/docs/reference/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,9 @@ Type: `object | undefined`, defaults to `undefined`.

Possible values are:
- `'netlify'`: Generate the security headers for static content on Netlify.

#### `securityHeaders.removeHtmlExtension`

If set, it will remove the `.html` extension from the URLs.

Type: `boolean`, defaults to `false`.
9 changes: 8 additions & 1 deletion docs/src/content/docs/ru/reference/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Content-Security-Policy (CSP) для динамического контента

### `sri.hashesModule`

Указывает путь к автоматически сгенерированному модулю, который содержит и
Указывает путь к автоматически сгенерированному модулю, который содержит и
кспортирует хеши SRI, вычисленные Astro-Shield для нашего контента.

Мы можем импортировать этот модуль в наш собственный код, если нам нужно
Expand Down Expand Up @@ -201,3 +201,10 @@ Content-Security-Policy (CSP) для статического и/или дина

Возможные значения:
- `'netlify'`: генерировать заголовки безопасности для статического контента на Netlify.

#### `securityHeaders.removeHtmlExtension`

Если установлено, то Astro-Shield будет удалять расширение `.html` из
URL-адресов.

Тип: `boolean`, defaults to `false`.