Skip to content

Commit

Permalink
feat(core): allow to unset the title tag (#3714)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Oct 14, 2024
1 parent 3c1a145 commit 50ade94
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
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: '',
},
};
```

0 comments on commit 50ade94

Please sign in to comment.