Skip to content

Commit

Permalink
feat: add base as option (#81)
Browse files Browse the repository at this point in the history
* Add base element as an option

* Add automatically generated api documentation

* Update e2e tests and override unit test
  • Loading branch information
costh-netm committed Jun 30, 2023
1 parent 2fa64fa commit 1cb7303
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docs/api/gatsby-plugin-next-seo.baseseoprops.base.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [gatsby-plugin-next-seo](./gatsby-plugin-next-seo.md) &gt; [BaseSeoProps](./gatsby-plugin-next-seo.baseseoprops.md) &gt; [base](./gatsby-plugin-next-seo.baseseoprops.base.md)

## BaseSeoProps.base property

Specifies the base URL to use for all relative URLs in a document. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

<b>Signature:</b>

```typescript
base?: BaseProps;
```
1 change: 1 addition & 0 deletions docs/api/gatsby-plugin-next-seo.baseseoprops.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface BaseSeoProps

| Property | Type | Description |
| --- | --- | --- |
| [base](./gatsby-plugin-next-seo.baseseoprops.base.md) | BaseProps | Specifies the base URL to use for all relative URLs in a document. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base |
| [canonical](./gatsby-plugin-next-seo.baseseoprops.canonical.md) | string | Set the page canonical url. |
| [description](./gatsby-plugin-next-seo.baseseoprops.description.md) | string | Set the page meta description. |
| [facebook](./gatsby-plugin-next-seo.baseseoprops.facebook.md) | { appId: string; } | Used for Facebook Insights, you must add a facebook app ID to your page to for it. |
Expand Down
2 changes: 1 addition & 1 deletion docs/api/gatsby-plugin-next-seo.gatsbyseo.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This component render the tags in the `<head>` for SEO on a per page basis. As a
<b>Signature:</b>

```typescript
GatsbySeo: ({ metaTags, linkTags, canonical, description, facebook, htmlAttributes, language, languageAlternates, mobileAlternate, nofollow, noindex, openGraph, title, titleTemplate, twitter, }: GatsbySeoProps) => JSX.Element
GatsbySeo: ({ metaTags, linkTags, canonical, description, facebook, htmlAttributes, language, languageAlternates, mobileAlternate, nofollow, noindex, openGraph, title, titleTemplate, twitter, base, }: GatsbySeoProps) => JSX.Element
```

## Remarks
Expand Down
9 changes: 8 additions & 1 deletion docs/etc/gatsby-plugin-next-seo.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ export interface ArticleJsonLdProps extends DeferSeoProps, Overrides<Article> {
url: string;
}

// Warning: (ae-internal-missing-underscore) The name "BaseProps" should be prefixed with an underscore because the declaration is marked as @internal
//
// @internal (undocumented)
export type BaseProps = JSX.IntrinsicElements['base'];

// @public
export const BaseSeo: ({ defer, metaTags, linkTags, ...props }: AllSeoProps) => JSX.Element;

// @public (undocumented)
export interface BaseSeoProps {
// Warning: (ae-incompatible-release-tags) The symbol "base" is marked as @public, but its signature references "BaseProps" which is marked as @internal
base?: BaseProps;
canonical?: string;
description?: string;
facebook?: {
Expand Down Expand Up @@ -216,7 +223,7 @@ export interface FAQJsonLdProps extends DeferSeoProps, Overrides<FAQPage> {
}

// @public
export const GatsbySeo: ({ metaTags, linkTags, canonical, description, facebook, htmlAttributes, language, languageAlternates, mobileAlternate, nofollow, noindex, openGraph, title, titleTemplate, twitter, }: GatsbySeoProps) => JSX.Element;
export const GatsbySeo: ({ metaTags, linkTags, canonical, description, facebook, htmlAttributes, language, languageAlternates, mobileAlternate, nofollow, noindex, openGraph, title, titleTemplate, twitter, base, }: GatsbySeoProps) => JSX.Element;

// @public (undocumented)
export interface GatsbySeoPluginOptions extends DefaultSeoProps, BaseSeoProps {
Expand Down
8 changes: 8 additions & 0 deletions e2e/__tests__/seo.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ test.each(testIterator)('Default SEO / - %s', async (_, disableJavascript) => {
const $document = await launch({ disableJavascript, path: '/' });
const tagAssertions = [
{ selector: 'h1', prop: 'innerText', result: 'Default SEO on this Page' },
{ selector: 'base', prop: 'href', result: 'http://www.test.com/' },
{ selector: 'base', prop: 'target', result: '_blank' },
{
selector: 'head title',
prop: 'innerText',
Expand Down Expand Up @@ -116,6 +118,12 @@ test.each(testIterator)(

const tagAssertions: TagAssertionBuilder[] = [
{ selector: 'h1', prop: 'innerText', result: 'Overridden Seo' },
{
selector: 'base',
prop: 'href',
result: 'http://www.overridetest.com/',
},
{ selector: 'base', prop: 'target', result: '_self' },
{
selector: 'html',
prop: 'lang',
Expand Down
4 changes: 4 additions & 0 deletions example/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ module.exports = {
{
resolve: 'gatsby-plugin-next-seo',
options: {
base: {
href: 'http://www.test.com',
target: '_blank',
},
title: 'Title A',
titleTemplate: '%s | Gatsby SEO',
description: 'Description A',
Expand Down
4 changes: 4 additions & 0 deletions example/src/pages/overridden.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const Overridden = () => (
noindex={true}
nofollow={true}
title='Title B'
base={{
href: 'http://www.overridetest.com',
target: '_self',
}}
description='Description B'
canonical='https://www.canonical.ie/b'
language='en'
Expand Down
5 changes: 5 additions & 0 deletions src/meta/__tests__/__snapshots__/base-seo.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,11 @@ exports[`renders correctly 1`] = `
<title>
This is a test title.
</title>
<base
data-rh="true"
href="www.test.com"
target="_blank"
/>
<link
data-rh="true"
href="https://m.canonical.ie"
Expand Down
24 changes: 24 additions & 0 deletions src/meta/__tests__/base-seo.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { __resetDefaults, BaseSeo } from '../base-seo';

const SEO = {
title: 'This is a test title.',
base: {
href: 'www.test.com',
target: '_blank',
},
language: 'en-GB',
description: 'This is a test description.',
canonical: 'https://www.canonical.ie',
Expand Down Expand Up @@ -71,6 +75,13 @@ test('returns full array for default seo object', () => {
SEO.language,
);

expect(document.querySelector('base')?.getAttribute('href')).toBe(
SEO?.base?.href,
);
expect(document.querySelector('base')?.getAttribute('target')).toBe(
SEO?.base?.target,
);

const title = getByText(
document.documentElement,
(content, element) =>
Expand Down Expand Up @@ -315,9 +326,15 @@ test('BaseSeo respects the nesting/overriding behaviour of React Helmet', () =>
const title = 'Example Title';
const exampleUrlBase = 'https://example.com';
const exampleUrlOverride = 'https://examp2le.com';
const exampleBaseHrefOverride = 'https://overridetest.com';
const exampleBaseTargetOverride = '_self';
render(
<>
<BaseSeo
base={{
href: exampleBaseHrefOverride,
target: exampleBaseTargetOverride,
}}
title={title}
titleTemplate={`${template} | %s`}
openGraph={{ url: exampleUrlBase }}
Expand Down Expand Up @@ -351,6 +368,13 @@ test('BaseSeo respects the nesting/overriding behaviour of React Helmet', () =>
);
expect(ogUrlTag).toBeTruthy();
expect(ogUrlTag?.getAttribute('content')).toEqual(exampleUrlOverride);

expect(document.querySelector('base')?.getAttribute('href')).toBe(
exampleBaseHrefOverride,
);
expect(document.querySelector('base')?.getAttribute('target')).toBe(
exampleBaseTargetOverride,
);
});

const ArticleSEO = {
Expand Down
4 changes: 4 additions & 0 deletions src/meta/base-seo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ export const BaseSeo = ({
helmetProps['titleTemplate'] = props.titleTemplate;
}

if (props.base) {
helmetProps['base'] = props.base;
}

return (
<>
<Helmet {...helmetProps}></Helmet>
Expand Down
3 changes: 3 additions & 0 deletions src/meta/gatsby-seo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import React from 'react';

import { GatsbySeoProps } from '../types';
Expand Down Expand Up @@ -39,6 +40,7 @@ export const GatsbySeo = ({
title,
titleTemplate,
twitter,
base,
}: GatsbySeoProps) => {
return (
<BaseSeo
Expand All @@ -57,6 +59,7 @@ export const GatsbySeo = ({
title={title}
titleTemplate={titleTemplate}
twitter={twitter}
base={base}
/>
);
};
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ interface HTMLAttributes {
* @public
*/
export interface BaseSeoProps {
/**
* Specifies the base URL to use for all relative URLs in a document. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
*/
base?: BaseProps;

/**
* The language being used for the current page.
*
Expand Down Expand Up @@ -592,3 +597,7 @@ export type StyleProps = JSX.IntrinsicElements['style'];
* @internal
*/
export type TitleProps = JSX.IntrinsicElements['title'];
/**
* @internal
*/
export type BaseProps = JSX.IntrinsicElements['base'];

0 comments on commit 1cb7303

Please sign in to comment.