- 
                Notifications
    You must be signed in to change notification settings 
- Fork 178
Feat: Add dev overrides #689
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
                    Changes from 5 commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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,5 @@ | ||
| --- | ||
| "@opennextjs/aws": minor | ||
| --- | ||
|  | ||
| Added some override for debugging OpenNext locally | 
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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,16 @@ | ||
| import fs from "node:fs"; | ||
| import type { ImageLoader } from "types/overrides"; | ||
|  | ||
| export default { | ||
| name: "fs-dev", | ||
| load: async (url: string) => { | ||
| const basePath = "../../assets"; | ||
| const body = fs.createReadStream(`${basePath}/${url}`); | ||
| const contentType = url.endsWith(".png") ? "image/png" : "image/jpeg"; | ||
| return { | ||
| body, | ||
| contentType, | ||
| cacheControl: "public, max-age=31536000, immutable", | ||
| }; | ||
| }, | ||
| } satisfies ImageLoader; | 
        
          
          
            36 changes: 36 additions & 0 deletions
          
          36 
        
  packages/open-next/src/overrides/incrementalCache/fs-dev.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,36 @@ | ||
| import type { IncrementalCache } from "types/overrides.js"; | ||
|  | ||
| import fs from "node:fs/promises"; | ||
| import path from "node:path"; | ||
|  | ||
| const buildId = process.env.NEXT_BUILD_ID; | ||
| const basePath = path.resolve(process.cwd(), `../../cache/${buildId}`); | ||
|  | ||
| const getCacheKey = (key: string) => { | ||
| return path.join(basePath, `${key}.cache`); | ||
| }; | ||
|  | ||
| const cache: IncrementalCache = { | ||
| name: "fs-dev", | ||
| get: async (key: string) => { | ||
| const fileData = await fs.readFile(getCacheKey(key), "utf-8"); | ||
| const data = JSON.parse(fileData); | ||
| const { mtime } = await fs.stat(getCacheKey(key)); | ||
| return { | ||
| value: data, | ||
| lastModified: mtime.getTime(), | ||
| }; | ||
| }, | ||
| set: async (key, value, isFetch) => { | ||
| const data = JSON.stringify(value); | ||
| const cacheKey = getCacheKey(key); | ||
| // We need to create the directory before writing the file | ||
| await fs.mkdir(path.dirname(cacheKey), { recursive: true }); | ||
| await fs.writeFile(cacheKey, data); | ||
| }, | ||
| delete: async (key) => { | ||
| await fs.rm(getCacheKey(key)); | ||
| }, | ||
| }; | ||
|  | ||
| export default cache; | 
  
    
      This file contains hidden or 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,21 @@ | ||
| import type { Queue } from "types/overrides.js"; | ||
|  | ||
| const queue: Queue = { | ||
| name: "dev-queue", | ||
| send: async (message) => { | ||
| const prerenderManifest = (await import("../../adapters/config/index.js")) | ||
| .PrerenderManifest as any; | ||
| const { host, url } = message.MessageBody; | ||
| const protocol = host.includes("localhost") ? "http" : "https"; | ||
| const revalidateId: string = prerenderManifest.preview.previewModeId; | ||
| await globalThis.internalFetch(`${protocol}://${host}${url}`, { | ||
| method: "HEAD", | ||
| headers: { | ||
| "x-prerender-revalidate": revalidateId, | ||
| "x-isr": "1", | ||
| }, | ||
| }); | ||
| }, | ||
| }; | ||
|  | ||
| export default queue; | 
  
    
      This file contains hidden or 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,54 @@ | ||
| import type { TagCache } from "types/overrides"; | ||
|  | ||
| import fs from "node:fs"; | ||
|  | ||
| const tagFile = "../../dynamodb-provider/dynamodb-cache.json"; | ||
|  | ||
| const tagContent = fs.readFileSync(tagFile, "utf-8"); | ||
|  | ||
| let tags = JSON.parse(tagContent) as { | ||
| tag: { S: string }; | ||
| path: { S: string }; | ||
| revalidatedAt: { N: string }; | ||
| }[]; | ||
|  | ||
| const tagCache: TagCache = { | ||
| name: "fs-dev", | ||
| getByPath: async (path: string) => { | ||
| return tags | ||
| .filter((tagPathMapping) => tagPathMapping.path.S === path) | ||
| .map((tag) => tag.tag.S); | ||
| }, | ||
| getByTag: async (tag: string) => { | ||
| return tags | ||
| .filter((tagPathMapping) => tagPathMapping.tag.S === tag) | ||
| .map((tag) => tag.path.S); | ||
| }, | ||
| getLastModified: async (path: string, lastModified?: number) => { | ||
| const revalidatedTags = tags.filter( | ||
| (tagPathMapping) => | ||
| tagPathMapping.path.S === path && | ||
| Number.parseInt(tagPathMapping.revalidatedAt.N) > (lastModified ?? 0), | ||
| ); | ||
| return revalidatedTags.length > 0 ? -1 : (lastModified ?? Date.now()); | ||
| }, | ||
| writeTags: async (newTags) => { | ||
| const unchangedTags = tags.filter( | ||
| (tagPathMapping) => | ||
| !newTags.some( | ||
| (tag) => | ||
| tag.tag === tagPathMapping.tag.S && | ||
| tag.path === tagPathMapping.path.S, | ||
| ), | ||
| ); | ||
| tags = unchangedTags.concat( | ||
| newTags.map((tag) => ({ | ||
| tag: { S: tag.tag }, | ||
| path: { S: tag.path }, | ||
| revalidatedAt: { N: String(tag.revalidatedAt) }, | ||
| })), | ||
| ); | ||
| }, | ||
| }; | ||
|  | ||
| export default tagCache; | ||
  
    
      This file contains hidden or 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 | 
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| import type { InternalEvent, StreamCreator } from "types/open-next"; | ||
| import type { Wrapper, WrapperHandler } from "types/overrides"; | ||
|  | ||
| const dummyWrapper: WrapperHandler = async () => async () => undefined; | ||
| const dummyWrapper: WrapperHandler = async (handler, converter) => { | ||
| return async (event: InternalEvent, responseStream?: StreamCreator) => { | ||
| return await handler(event, responseStream); | ||
| }; | ||
| }; | ||
|  | ||
| export default { | ||
| name: "dummy", | ||
| wrapper: dummyWrapper, | ||
| supportStreaming: false, | ||
| supportStreaming: true, | ||
| } satisfies Wrapper; | 
  
    
      This file contains hidden or 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,57 @@ | ||
| import express from "express"; | ||
|  | ||
| import type { StreamCreator } from "types/open-next.js"; | ||
| import type { WrapperHandler } from "types/overrides.js"; | ||
|  | ||
| const wrapper: WrapperHandler = async (handler, converter) => { | ||
| const app = express(); | ||
| // To serve static assets | ||
| app.use(express.static("../../assets")); | ||
|  | ||
| const imageHandlerPath = "../../image-optimization-function/index.mjs"; | ||
| const imageHandler = await import(imageHandlerPath).then((m) => m.handler); | ||
|  | ||
| app.all("/_next/image", async (req, res) => { | ||
| const internalEvent = await converter.convertFrom(req); | ||
| const _res: StreamCreator = { | ||
| writeHeaders: (prelude) => { | ||
| res.writeHead(prelude.statusCode, prelude.headers); | ||
| return res; | ||
| }, | ||
| }; | ||
| await imageHandler(internalEvent, _res); | ||
| }); | ||
|  | ||
| app.all("*paths", async (req, res) => { | ||
| const internalEvent = await converter.convertFrom(req); | ||
| const _res: StreamCreator = { | ||
| writeHeaders: (prelude) => { | ||
| res.writeHead(prelude.statusCode, prelude.headers); | ||
| return res; | ||
| }, | ||
| onFinish: () => {}, | ||
| }; | ||
| await handler(internalEvent, _res); | ||
| }); | ||
|  | ||
| const server = app.listen( | ||
| Number.parseInt(process.env.PORT ?? "3000", 10), | ||
| () => { | ||
| console.log(`Server running on port ${process.env.PORT ?? 3000}`); | ||
| }, | ||
| ); | ||
|  | ||
| app.on("error", (err) => { | ||
| console.error("error", err); | ||
| }); | ||
|  | ||
| return () => { | ||
| server.close(); | ||
| }; | ||
| }; | ||
|  | ||
| export default { | ||
| wrapper, | ||
| name: "expresss-dev", | ||
| supportStreaming: true, | ||
| }; | 
  
    
      This file contains hidden or 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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.