Skip to content

fix: publish from subdirectory #1756

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

Merged
merged 5 commits into from
Nov 11, 2022
Merged

fix: publish from subdirectory #1756

merged 5 commits into from
Nov 11, 2022

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Nov 10, 2022

Summary

Currently we expect Next.js to publish the distDir (i.e. .next). This is how we ascertain how to find the build files, particularly required-server-files.json. However most of the generated outputs are not created in the correct structure, so we need to copy or move them there as part of the build. This has worked fine so far, but has some important drawbacks. The main one is that it means the whole folder is uploaded as part of the deploy, even though most of it is not needed - and in fact needs 404 redirects to avoid making them publicly available. This hasn't been a major problem in the past (once the first deploy has been uploaded there aren't many extra files that need uploading). However Next 13 has started generating very large cache files that change on every build, which are now being uploaded every time.

Since this plugin was created it is now possible to change the publish dir from within a build plugin. This gives us another option that retains the best of these worlds. We can use .next as the default publish dir, but during the build change it to a hidden .next/dist dir, which then contains the actual files.

Test plan

  1. Ensure that the deploy previews work

Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal

image

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality
  • Add docs when necessary

🧪 Once merged, make sure to update the version if needed and that it was published correctly.

@netlify
Copy link

netlify bot commented Nov 10, 2022

Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

Name Link
🔨 Latest commit ba479be
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/636e16c0170cf40008ae7ccc
😎 Deploy Preview https://deploy-preview-1756--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 10, 2022

Deploy Preview for netlify-plugin-nextjs-export-demo ready!

Name Link
🔨 Latest commit ba479be
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/636e16c03d717700083b4c72
😎 Deploy Preview https://deploy-preview-1756--netlify-plugin-nextjs-export-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions github-actions bot added the type: bug code to address defects in shipped code label Nov 10, 2022
@netlify
Copy link

netlify bot commented Nov 10, 2022

Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

Name Link
🔨 Latest commit ba479be
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/636e16c0324fef00095cbc56
😎 Deploy Preview https://deploy-preview-1756--netlify-plugin-nextjs-static-root-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 10, 2022

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit ba479be
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/636e16c083e79f0008e45062
😎 Deploy Preview https://deploy-preview-1756--next-plugin-canary.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 10, 2022

Deploy Preview for nextjs-plugin-custom-routes-demo ready!

Name Link
🔨 Latest commit ba479be
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/636e16c0170cf40008ae7cd6
😎 Deploy Preview https://deploy-preview-1756--nextjs-plugin-custom-routes-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 10, 2022

Deploy Preview for netlify-plugin-nextjs-demo ready!

Name Link
🔨 Latest commit ba479be
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/636e16c0324fef00095cbc60
😎 Deploy Preview https://deploy-preview-1756--netlify-plugin-nextjs-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 10, 2022

Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!

Name Link
🔨 Latest commit ba479be
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/636e16c0122dca0009b66cab
😎 Deploy Preview https://deploy-preview-1756--netlify-plugin-nextjs-next-auth-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 10, 2022

Deploy Preview for next-plugin-edge-middleware ready!

Name Link
🔨 Latest commit ba479be
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/636e16c0324fef00095cbc5b
😎 Deploy Preview https://deploy-preview-1756--next-plugin-edge-middleware.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 10, 2022

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit ba479be
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/636e16c03d717700083b4c77
😎 Deploy Preview https://deploy-preview-1756--next-i18next-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@ascorbic ascorbic marked this pull request as ready for review November 10, 2022 14:05
@ascorbic ascorbic requested a review from a team November 10, 2022 14:05
@@ -25,14 +25,6 @@ import {
const matchesMiddleware = (middleware: Array<string>, route: string): boolean =>
middleware.some((middlewarePath) => route.startsWith(middlewarePath))

const generateHiddenPathRedirects = ({ basePath }: Pick<NextConfig, 'basePath'>): NetlifyConfig['redirects'] =>
HIDDEN_PATHS.map((path) => ({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not needed anymore, as these aren't published

Choose a reason for hiding this comment

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

What are/were HIDDEN_PATHS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They were the files in .next that we didn't want to be publicly-visible in the deploy, such as cache, server and various json files

@@ -66,21 +58,6 @@ const generateLocaleRedirects = ({
return redirects
}

export const generateStaticRedirects = ({
netlifyConfig,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need to redirect, because the files are in the right place


if (i18n) {
netlifyConfig.redirects.push({ from: `${basePath}/:locale/_next/static/*`, to: `/static/:splat`, status: 200 })
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems i18n still loads from /_next in the root, so it's not needed

}): Promise<void> => {
console.log('Moving static page files to serve from CDN...')
const outputDir = join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless')
const outputDir = join(distDir, 'server')

Choose a reason for hiding this comment

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

Why have we removed serverless from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We force builds to use server target anyway, and in Next 13 serverless breaks the build.

Copy link

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

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

Looks good @ascorbic! 🚀

@kodiakhq kodiakhq bot merged commit 1faf191 into main Nov 11, 2022
@kodiakhq kodiakhq bot deleted the mk/publish-dir branch November 11, 2022 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants