-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: customize landing page via
landingPage
(#3332)
* feat: customize landing page via `landingPage` * Fix TS * Comment * Docs * Add example to the changeset * Fix image link
- Loading branch information
Showing
8 changed files
with
179 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
'graphql-yoga': minor | ||
--- | ||
|
||
Customize the landing page by passing a custom renderer that returns `Response` to the `landingPage` | ||
option | ||
|
||
```ts | ||
import { createYoga } from 'graphql-yoga' | ||
|
||
const yoga = createYoga({ | ||
landingPage: ({ url, fetchAPI }) => { | ||
return new fetchAPI.Response( | ||
/* HTML */ ` | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>404 Not Found</title> | ||
</head> | ||
<body> | ||
<h1>404 Not Found</h1> | ||
<p>Sorry, the page (${url.pathname}) you are looking for could not be found.</p> | ||
</body> | ||
</html> | ||
`, | ||
{ | ||
status: 404, | ||
headers: { | ||
'Content-Type': 'text/html' | ||
} | ||
} | ||
) | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
description: Learn more about landing page customization in GraphQL Yoga | ||
--- | ||
|
||
# Landing Page | ||
|
||
When GraphQL Yoga hits 404, it returns a default landing page like below; | ||
|
||
![image](https://github.com/dotansimha/graphql-yoga/assets/20847995/7bc058db-1823-4316-86ff-cf1847522da5) | ||
|
||
If you don't expect to hit 404 in that path, you can configure `graphqlEndpoint` to avoid this page. | ||
|
||
```ts | ||
import { createYoga } from 'graphql-yoga' | ||
|
||
const yoga = createYoga({ | ||
graphqlEndpoint: '/my-graphql-endpoint' | ||
}) | ||
``` | ||
|
||
How ever you can disable the landing page, and just get 404 error directly by setting `landingPage` | ||
to `false`. | ||
|
||
```ts | ||
import { createYoga } from 'graphql-yoga' | ||
|
||
const yoga = createYoga({ | ||
landingPage: false | ||
}) | ||
``` | ||
|
||
You can also customize the landing page by passing a custom renderer that returns `Response` to the | ||
`landingPage` option. | ||
|
||
```ts | ||
import { createYoga } from 'graphql-yoga' | ||
|
||
const yoga = createYoga({ | ||
landingPage: ({ url, fetchAPI }) => { | ||
return new fetchAPI.Response( | ||
/* HTML */ ` | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>404 Not Found</title> | ||
</head> | ||
<body> | ||
<h1>404 Not Found</h1> | ||
<p>Sorry, the page (${url.pathname}) you are looking for could not be found.</p> | ||
</body> | ||
</html> | ||
`, | ||
{ | ||
status: 404, | ||
headers: { | ||
'Content-Type': 'text/html' | ||
} | ||
} | ||
) | ||
} | ||
}) | ||
``` | ||
|
||
<Callout> | ||
`Response` is part of the Fetch API, so if you want to learn more about it, you can check the [MDN | ||
documentation](https://developer.mozilla.org/en-US/docs/Web/API/Response). | ||
</Callout> |