Skip to content
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

Dynamic asset prefix inside static pages #65

Merged

Conversation

megazazik
Copy link
Contributor

This PR adds support of dynamicAssetPrefix in statically generated pages. See details here
#63 (comment)

Copy link
Contributor

@peter-jozsa peter-jozsa left a comment

Choose a reason for hiding this comment

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

I have tested these changes in my app and in a test project too (with statically generated pages), everything worked as expected.

@arefaslani
Copy link
Member

@megazazik Could you just convert tabs to spaces? I didn't touch that because else it'll show my name in git blame, but you did the job :)

@megazazik
Copy link
Contributor Author

@arefaslani I thought I removed all tabs in the second commit. Are you sure there are tabs in the file still? I don't see them.

@arefaslani
Copy link
Member

@megazazik Ah sorry you're right. I was checking previous commit.

@arefaslani
Copy link
Member

arefaslani commented Jan 19, 2021

@megazazik This is my configuration file:

const withImages = require('next-images')
module.exports = withImages({
  esModule: true,
  assetPrefix: process.env.PUBLIC_URL,
  dynamicAssetPrefix: true,
  webpack(config, options) {
    return config
  }
})

I added this line in my hosts file to mimic CDN behavior:

cdn.example.com 127.0.0.1

I built the project using this command:

PUBLIC_URL=http://cdn.example.com:5000 yarn build

Then I started the project with this way and everything was fine:

yarn start -p 5000

But if I change the PUBLIC_URL variable to http://cdn2.example.com:5000 using this command:

PUBLIC_URL=http://cdn2.example.com:5000 yarn start -p 5000

It still shows the previous URL in chrome devtools. The URL that I used in build time. is this the expected behavior? Because I thought that you need to be able to change the asset prefix after build. Am I right?

@peter-jozsa
Copy link
Contributor

Is your page statically generated where you checked it? If it is then did you enabled static page revalidation?

export async function getStaticProps() {
  return {
    props: {},
    revalidate: 1,
  }
}

If you enabled it then after several refreshes your static pages will be re-generated with the new asset prefix values.

If you are only using it in server-side rendered pages then asset prefix should be changed immediately when you start up the server and fire your first request.

@arefaslani
Copy link
Member

@peter-jozsa I neither used getStaticProps nor getServerSideProps. Should I use one of these functions to make it work?

@peter-jozsa
Copy link
Contributor

As far as I know statically generated pages are generated during next build which means the value of assetPrefix during build time will be statically generated into the HTMLs. If you change it during runtime by PUBLIC_URL=http://cdn2.example.com:5000 yarn start -p 5000 in your case it will do nothing since value of assetPrefix was already evaluated during build time and its value is persisted in the static HTML. But if you enable revalidation by returning { revalidate: 1 } in your getStaticProps function of the page you are telling next.js to regenerate that given page at most once in 1 sec. During page regenration the same will happen like when you run next build so the current runtime value of assetPrefix will be generated into the HTML code of the static page and in the end the you should see requests targeting cdn2.example.com:5000 after a few refreshes.

@arefaslani arefaslani merged commit 62c6f90 into twopluszero:master Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants