Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
/frameworkPublic archive

fix(nuxt): allow responding with custom headers from error.vue - #8469

Merged
pi0 merged 4 commits into
mainfrom
fix/error-headers
Oct 26, 2022
Merged

fix(nuxt): allow responding with custom headers from error.vue#8469
pi0 merged 4 commits into
mainfrom
fix/error-headers

Conversation

@danielroe

Copy link
Copy Markdown
Member

🔗 Linked issue

follow-up to #7340, resolvesnuxt/nuxt#14895

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

This allows responding to an error within error.vue with more than just HTML and the original error code, such as modifying the code or redirecting to a different page.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@danielroedanielroe added bug Something isn't working 🍰 p2-nice-to-have Priority 2: nothing is broken but it's worth addressing labels Oct 25, 2022
@danielroe
danielroe requested a review from pi0October 25, 2022 14:17
@danielroedanielroe self-assigned this Oct 25, 2022
@codesandbox

Copy link
Copy Markdown

CodeSandbox logoCodeSandbox logo Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@netlify

netlifyBot commented Oct 25, 2022

Copy link
Copy Markdown

Deploy Preview for nuxt3-docs canceled.

NameLink
🔨 Latest commitfd9ea43
🔍 Latest deploy loghttps://app.netlify.com/sites/nuxt3-docs/deploys/63580a35d11c460008e1e81b

Comment threadpackages/nuxt/src/core/runtime/nitro/error.ts
Comment threadpackages/nuxt/src/core/runtime/nitro/error.ts
}
html = template(errorObject)
event.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
return event.res.end(template(errorObject))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please return string? H3 will add content-type too and also handles sending response string. Reducing req/res dependency

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. though note I just moved this up - this wasn't a change in this pr

@danielroedanielroeOct 25, 2022

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just tried - are you sure an error handler supports returning value directly? it seems to hang.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can check. But seems strange because it is a nitro API route right?


event.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
event.res.end(html)
event.respondWith(new H3Response(await res.text(), {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

respond with API is not stable yet and might change. Let's use raw methods for now. I will have an idea for a way to simplify and standardized response format for nitro...

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good!

pi0
pi0 approved these changes Oct 25, 2022
@pi0
pi0 merged commit 12808f1 into mainOct 26, 2022
@pi0
pi0 deleted the fix/error-headers branch October 26, 2022 08:25
@pi0pi0 mentioned this pull request Nov 3, 2022
@thijsw

Copy link
Copy Markdown

I'd like to use this feature but I'm not sure how to use it? Does anybody might want to share an example of how I can redirect a user on the error page?

@danielroe

Copy link
Copy Markdown
MemberAuthor

@thijsw Would you create a discussion rather than commenting on a merged PR?

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

3.xbugSomething isn't working🍰 p2-nice-to-havePriority 2: nothing is broken but it's worth addressing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

allow setting headers or returning a redirect from error.vue

3 participants

@danielroe@thijsw@pi0