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(core): allow to unset the title tag #3714

Merged
merged 2 commits into from
Oct 14, 2024
Merged
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
4 changes: 2 additions & 2 deletions e2e/cases/html/title/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('should generate default title correctly', async () => {
expect(html).toContain('<title>Rsbuild App</title>');
});

test('should allow setting empty title to override default title', async () => {
test('should allow setting empty title to unset the default title', async () => {
const rsbuild = await build({
cwd: __dirname,
rsbuildConfig: {
Expand All @@ -34,7 +34,7 @@ test('should allow setting empty title to override default title', async () => {

const html =
files[Object.keys(files).find((file) => file.endsWith('foo.html'))!];
expect(html).toContain('<title></title>');
expect(html).not.toContain('<title>');
});

test('should generate title correctly', async () => {
Expand Down
16 changes: 9 additions & 7 deletions packages/core/src/rspack/RsbuildHtmlPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,15 @@ const applyTagConfig = (
};

const addTitleTag = (headTags: HtmlTagObject[], title = '') => {
headTags.unshift({
tagName: 'title',
innerHTML: title,
attributes: {},
voidTag: false,
meta: {},
});
if (title !== '' && title !== undefined) {
headTags.unshift({
tagName: 'title',
innerHTML: title,
attributes: {},
voidTag: false,
meta: {},
});
}
};

export class RsbuildHtmlPlugin {
Expand Down
12 changes: 12 additions & 0 deletions website/docs/en/config/html/title.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ export default {
},
};
```

## Unset `<title>` Tag

When `html.title` is set to an empty string, Rsbuild will not inject the `<title>` tag:

```js
export default {
html: {
title: '',
},
};
```
16 changes: 14 additions & 2 deletions website/docs/zh/config/html/title.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
如果当前项目使用的 HTML 模板中已经包含了 `<title>` 标签,那么 `html.title` 将不会生效。
:::

### 字符串用法
## 字符串用法

当 `html.title` 可以直接设置为一个字符串:

Expand All @@ -27,7 +27,7 @@ export default {
<title>Example</title>
```

### 函数用法
## 函数用法

- **类型:**

Expand Down Expand Up @@ -55,3 +55,15 @@ export default {
},
};
```

## 不注入 title 标签

当 `html.title` 设置为空字符串时,Rsbuild 不会注入 `<title>` 标签:

```js
export default {
html: {
title: '',
},
};
```
Loading